sql try/catch rollback/commit - preventing erroneous commit after rollback

后端 未结 5 658
渐次进展
渐次进展 2020-12-12 21:41

I am trying to write an MS sql script that has a transaction and a try/catch block. If it catches an exception, the transaction is rolled back. If not, the transaction is

5条回答
  •  伪装坚强ぢ
    2020-12-12 22:22

    Transaction counter

    --@@TRANCOUNT = 0
    begin try
    --@@TRANCOUNT = 0
    BEGIN TRANSACTION tran1
     --@@TRANCOUNT = 1
    
            --your code
            -- if failed  @@TRANCOUNT = 1
            -- if success @@TRANCOUNT = 0
    
    COMMIT TRANSACTION tran1
    
    end try
    
    begin catch
        print 'FAILED'
    end catch
    

提交回复
热议问题