View code
Declare @ Maxlsh Bigint Declare @ Newmaxlsh Bigint Declare @ SQL Nvarchar ( 1000 ) Set @ Maxlsh = 0 Set @ Newmaxlsh = 0 Set @ SQL = '' Select @ Maxlsh = Isnull ( Max (ID ), 0 ) From Kb_lable_isscansync Select @ Newmaxlsh = Lsh From Kb_lable_scan Where Lsh > = @ Maxlsh If @ Newmaxlsh > @ Maxlsh Begin Set @ SQL = ' Begin transaction; begin try update kb_lable_temp set isscan = '' Yes '' From kb_lable_temp t inner join kb_lable_scan s on T. ID = S. ID where S. lsh> = ' + Cast ( @ Maxlsh As Nvarchar ( 20 )) + ' And (T. isscan is null or T. isscan <> '' Yes '' ); Insert into kb_lable_isscansync (ID, addtime, issync) values ( ' + Cast ( @ Newmaxlsh As Nvarchar ( 20 )) + ' , Getdate (), 1); end try begin catch if trancount> 0 rollback transaction; end catch; If trancount> 0 commit transaction; ' Exec ( @ SQL ) End
Try catch transaction for a T-SQLCode