xact_abort on vs begin transaction

Heisenberg 261 Reputation points
2022-07-21T20:51:16.413+00:00

hi folks,
Whats the difference between setting xact_abort on vs begin transaction in a batch? do they behave same ?

SQL Server | Other
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Erland Sommarskog 121.9K Reputation points MVP Volunteer Moderator
    2022-07-22T22:10:09.283+00:00

    It's like the difference between an apple and bicycle. That is, they are not the same at all.

    BEGIN TRANSACTION starts a transaction, and this transaction will remain option until it commits or rolls back. In the latter cases, all updates performed after BEGIN TRANSACTION will be rolled back. Also, until, you commit the transaction, the changes performed in the transaction is only visible to the process running the transaction.

    SET XACT_ABORT ON does not start a transactions, so subsequent updates are visible to all processes and cannot be rolled back.

    SET XACT_ABORT ON controls what happens on an error. With the default setting, XACT_ABORT OFF, any of these things can happen if there is an error during execution:

    1. The connection is closed. (Internal error in SQL Server.)
    2. Execution is aborted, and any open transaction is not rolled back.
    3. Execution is aborted, but any transaction is left open.
    4. The current scope is aborted, and execution continues on the next statement in the invoking scope. There is no roll back.
    5. Execution continues on the next statement. The statement is rolled back, but any open transaction is not.

    With XACT_ABORT ON, only 1 and 2 can happen. Well, almost. There are a few errors, where XACT_ABORT is ignored, and nothing is terminated.

    1 person found this answer helpful.

  2. CathyJi-MSFT 22,396 Reputation points Microsoft External Staff
    2022-07-22T03:09:29.603+00:00

    Hi @Heisenberg ,

    No, they are not same. Please reading below similar threads, you get the difference between them, hope they could help you.

    What is the point of TRY CATCH block when XACT_ABORT is turned ON?
    How to use SET XACT_ABORT ON the right way


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments

  3. Heisenberg 261 Reputation points
    2022-07-26T18:46:31.5+00:00

    Thank you all.
    @Erland Sommarskog currently im using block like this, In this while loop im doing insert and then delete in batch of 5000 rows. Currently if for some reason DELETE statement fails INSERT succeeds for any loop iteration, i want insert to rollback if delete fails for that iteration. Can I achieve this using set xact_abort on in the beginning?

    begin try  
      
    while loop  
    begin  
    insert into table...  
      
    delete from table...  
    end  
    end try  
    begin catch  
    if @@trancount > 0  
    rollback  
    end catch  
    

  4. Tom Phillips 17,771 Reputation points
    2022-07-26T19:04:10.907+00:00

    You should be using a transaction inside your loop

    begin try  
          
     while loop  
     begin  
    BEGIN TRAN  
     insert into table...  
          
     delete from table...  
    COMMIT  
     end  
     end try  
     begin catch  
     if @@trancount > 0  
     rollback  
     end catch  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.