Rolls back an explicit or implicit transaction to the beginning of the transaction, or to a savepoint inside a transaction.
ROLLBACK [TRAN[SACTION] [transaction_name |
@tran_name_variable | savepoint_name | @savepoint_variable] ]
ROLLBACK TRANSACTION erases all data modifications made since the start of the transaction or to a savepoint. It also frees resources held by the transaction.
ROLLBACK TRANSACTION without a savepoint_name or transaction_name rolls back to the beginning of the transaction. When nesting transactions, this same statement rolls back all inner transactions to the outermost BEGIN TRANSACTION statement. In both cases, ROLLBACK TRANSACTION decrements the @@TRANCOUNT system function to 0. ROLLBACK TRANSACTION savepoint_name does not decrement @@TRANCOUNT.
A ROLLBACK TRANSACTION statement specifying a savepoint_name does not free any locks.
ROLLBACK TRANSACTION cannot reference a savepoint_name in distributed transactions started either explicitly with BEGIN DISTRIBUTED TRANSACTION or escalated from a local transaction.
A transaction cannot be rolled back after a COMMIT TRANSACTION statement is executed.
Within a transaction, duplicate savepoint names are allowed, but a ROLLBACK TRANSACTION using the duplicate savepoint name rolls back only to the most recent SAVE TRANSACTION using that savepoint name.
In stored procedures, ROLLBACK TRANSACTION statements without a savepoint_name or transaction_name roll back all statements to the outermost BEGIN TRANSACTION. A ROLLBACK TRANSACTION statement in a stored procedure that causes @@TRANCOUNT to have a different value when the trigger completes than the @@TRANCOUNT value when the stored procedure was called produces an informational message. This message does not affect subsequent processing.
If a ROLLBACK TRANSACTION is issued in a trigger:
@@TRANCOUNT is incremented by one when entering a trigger, even when in autocommit mode. (The system treats a trigger as an implied nested transaction.)
ROLLBACK TRANSACTION statements in stored procedures do not affect subsequent statements in the batch that called the procedure; subsequent statements in the batch are executed. ROLLBACK TRANSACTION statements in triggers terminate the batch containing the statement that fired the trigger; subsequent statements in the batch are not executed.
A ROLLBACK TRANSACTION statement does not produce any messages to the user. If warnings are needed in stored procedures or triggers, use the RAISERROR or PRINT statements. RAISERROR is the preferred statement for indicating errors.
The effect of a ROLLBACK on cursors is defined by these three rules:
ROLLBACK TRANSACTION permissions default to any valid user.
BEGIN DISTRIBUTED TRANSACTION | COMMIT WORK |
BEGIN TRANSACTION | ROLLBACK WORK |
Cursor Locking | SAVE TRANSACTION |
COMMIT TRANSACTION | Transactions |