T-SQL事务编写
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=''是'' 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<> ''是''); 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
一个T-SQL 的Try Catch事务代码