sql proc触发异常处理回滚 针对proc嵌套proc很有用

 

 

begin
begin try
    begin tran
        update table set col = '' where a = 1
        IF @@Rowcount = 0--判断错误
            BEGIN
                --ROLLBACK TRAN
                SET @vcResult = '添加失败'
                RAISERROR(@vcResult,16,1)
            END
    commit tran
end try
begin catch
IF @@tranCount = 1    ROLLBACK TRAN ;  --@@tranCount = 1 代表开启事物的个数,如果嵌套调用开启了两个事物,则在外部事物回滚
end catch
end