The judgment in favor of the defendant against the plaintiff for the damages assessed and the costs may be pleaded by the defendant as a payment in the same action, or in any other suit on the same cause of action on which the attachment was sued out. An execution shall not be issued on such judgment until after the dismissal of the action or final judgment on the merits. If the action be dismissed, or if, upon a trial on the merits, judgment be in favor of the defendant, without his having obtained the benefit of the judgment in his favor for damages and costs, he shall have execution of said judgment in his favor. If the plaintiff recover in the trial on the merits, and on such trial the defendant did not avail of his judgment against the plaintiff for damages and costs, the greater recovery shall be credited with the sum of the smaller, and judgment shall be rendered by the court in favor of the party to whom the difference may be due.