Exit and rollback everything in script on error

后端 未结 5 1662
我寻月下人不归
我寻月下人不归 2021-02-15 15:43

I have a TSQL script that does a lot of database structure adjustments but it\'s not really safe to just let it go through when something fails.

to make things clear:

5条回答
  •  广开言路
    2021-02-15 16:35

    SET XACT_ABORT ON
    BEGIN TRAN
    
    -- Batch 1
    
    GO
    
    if @@TRANCOUNT = 0 
    SET NOEXEC ON;
    GO
    
    -- Batch 2
    
    GO
    
    if @@TRANCOUNT = 0 
    SET NOEXEC ON;
    GO
    
    -- Batch 3
    
    GO
    
    if @@TRANCOUNT > 0 
    COMMIT
    GO
    

提交回复
热议问题