Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rollback a transaction in a stored procedure?

Looking at the SQL Server Books Online, Microsoft seems to have an (incorrect) method of handling nested transactions in a stored procedure:

Nesting Transactions

Explicit transactions can be nested. This is primarily intended to support transactions in stored procedures that can be called either from a process already in a transaction or from processes that have no active transaction.

The example goes on to show a stored procedure that starts its own transaction ("The procedure enforces its transaction regardless of the transaction mode of any process that executes it."):

CREATE PROCEDURE TransProc @PriKey INT, @CharCol CHAR(3) AS
   BEGIN TRANSACTION InProc
      ...
   COMMIT TRANSACTION InProc;

This procedure can then either be called without a transaction running:

EXECUTE TransProc 3,'bbb';

Or with an explicit transaction:

BEGIN TRANSACTION OutOfProc;

EXEC TransProc 1, 'aaa';

COMMIT TRANSACTION OutOfProc

What they don't address is what happens when the stored produre:

  • fails with an error, but leaves the transaction running
  • fails with an error, but doesn't leave the transaction running
  • encounters an error, but continues executing with the transaction open
  • encounters an error, but continues executing with the transaction rolled back

There is no:

  • SET XACT_ABORT ON
  • @@TRANCOUNT

anywhere in the canonical example.

If i didn't know any better, i would have thought that the line:

The following example shows the intended use of nested transactions.

should actually read

The following example shows the how not to use nested transactions.

Unless someone can make heads or tails of this BOL example?

like image 928
Ian Boyd Avatar asked Jul 17 '12 21:07

Ian Boyd


People also ask

Can we use rollback in stored procedure?

Even in the case where an exception causes the stored procedure to abort, you must still use COMMIT or ROLLBACK after the procedure exits.

How do you rollback a transaction?

You can use ROLLBACK TRANSACTION to erase all data modifications made from the start of the transaction or to a savepoint. It also frees resources held by the transaction. This does not include changes made to local variables or table variables. These are not erased by this statement.

How do I rollback changes in SQL?

SQL RollBackROLLBACK is the SQL command that is used for reverting changes performed by a transaction. When a ROLLBACK command is issued it reverts all the changes since last COMMIT or ROLLBACK.

Can we roll back a transaction after it has committed?

COMMIT permanently saves the changes made by the current transaction. ROLLBACK undo the changes made by the current transaction. 2. The transaction can not undo changes after COMMIT execution.


1 Answers

You need to use the try catch block with the transaction. So in case you get the error in your catch block then you can rollback your transaction.

Please see the below sql server code for that.

BEGIN TRANSACTION;

BEGIN TRY
    -- Some code
    COMMIT TRANSACTION;
END TRY
BEGIN CATCH

    ROLLBACK TRANSACTION;
END CATCH;
like image 153
Mayur Desai Avatar answered Oct 12 '22 01:10

Mayur Desai