User Tools

Site Tools


technology:environments:sql:transaction

Transaction

T-SQL Transactions

A transaction is a single unit of work. If a transaction is successful, all of the data modifications made during the transaction are committed and become a permanent part of the database. If a transaction encounters errors and must be canceled or rolled back, then all of the data modifications are erased.

Tables are locked while transactions are active. When using explicit transactions; begins should always be followed by a commit or rollback to ensure the table is unlocked.

Begin Transaction

Marks the starting point of an explicit, local transaction. Explicit transactions start with the BEGIN TRANSACTION statement and end with the COMMIT or ROLLBACK statement.

BEGIN TRANSACTION

Rollback Transaction

Rolls back an explicit or implicit transaction to the beginning of the transaction, or to a savepoint inside the 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.

ROLLBACK TRANSACTION

Commit Transaction

Marks the end of a successful implicit or explicit transaction. If @@TRANCOUNT is 1, COMMIT TRANSACTION makes all data modifications since the start of the transaction a permanent part of the database, frees the transaction's resources, and decrements @@TRANCOUNT to 0. When @@TRANCOUNT is greater than 1, COMMIT TRANSACTION decrements @@TRANCOUNT only by 1 and the transaction stays active.

COMMIT TRANSACTION
technology/environments/sql/transaction.txt · Last modified: Wednesday, October 11, 2023 10:01 PM (external edit)