TRY and Catch Blocks

  • In the ms e-learning courses it says that to use try catch blocks you must set xact_abort on but in the manual there is no mention of this. Can anyone shed some light on whether you have use xact_abort on or not

    Ta

     

    David

  • It's not necessary. Some DDL command and SET XACT_ABORT ON can cause a transaction is terminated outside of the try catch. The transaction is in the status of uncommitable, which prevents any further log writing. You can check the transaction status by XACT_STATE() function and rollback it before doing any data change, e.g. logging the error.

    So you can always rollback the transaction in the catch block and do other things safely, regardless xact_abort is on or off.

    BEGIN CATCH

    IF @@TRANSACTION>0 ROLLBACK

    -- do other stuff

    END CATCH;

     

  • Thanks

     

    David

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply