⌘BABAKOV.NET Let's code!
100% [dark] [ru]

Two Ways to Execute Transactions in MSSQL with Rollback on Error.

Two Ways to Execute Transactions in MSSQL with Rollback on Error.

The First Way:

BEGIN TRY
    BEGIN TRAN
    UPDATE 1
    UPDATE 2 --Error
    UPDATE 3
    COMMIT TRAN
END TRY
BEGIN CATCH
    ROLLBACK TRAN
END CATCH

The Second Way:

SET XACT_ABORT ON
BEGIN TRAN
UPDATE 1
UPDATE 2 --Error
UPDATE 3
COMMIT TRAN

Both ways will do the same thing, in case of an error, the transaction will be rolled back.

Comments (0)

No comments yet.

Leave a comment
Comment
Images (up to 5 MB)
Your comment will appear after moderation.