遗忘海岸

江湖程序员 -Feiph(LM战士)

导航

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事务代码

posted on 2013-03-23 13:26  遗忘海岸  阅读(499)  评论(0编辑  收藏  举报