'What's the difference between begin transaction vs. begin work
I saw in SQL Server about the transaction part it has following statements:
- begin transaction or begin work
- commit transaction or commit work
- rollback transaction or commit work
I used transaction set statements in my work, but why there are work statements here? Are they same? If not, what the difference between them?
Solution 1:[1]
- According to Microsoft's SQL Server documentation, there is no support for
BEGIN WORK, instead useBEGIN TRANSACTION(which also supports named transactions).- The token
BEGIN(by itself) denotes a statement-block and is completely unrelated to transactions.
- The token
- Curiously, while SQL Server does not support
BEGIN WORK, SQL Server does supportCOMMIT WORKandROLLBACK WORK.COMMIT WORKhas identical semantics toCOMMIT TRANSACTION.ROLLBACK WORKhas identical semantics toROLLBACK TRANSACTION.
- Also curiously, [the ISO SQL-2016][6] grammar uses
START TRANSACTIONwithCOMMIT WORKandROLLBACK WORKwhich is a strangely inconsistent use of language keywords.- SQL Server 2017+ and Azure SQL does seem to support
START WORKthough SSMS does not recognize the syntax.
- SQL Server 2017+ and Azure SQL does seem to support
ISO SQL (2016) vs T-SQL (MS SQL Server)
| ISO SQL | T-SQL (SQL Server) | Effect on @@TRANCOUNT |
|
|---|---|---|---|
| Transactions | |||
| Begin a transaction | START TRANSACTION |
BEGIN TRANSACTION |
+ 1 |
| Commit a transaction | COMMIT WORK |
COMMIT TRANSACTION |
- 1 |
| Rollback a transaction | ROLLBACK WORK |
ROLLBACK TRANSACTION |
= 0 |
| Savepoints | |||
| Create a savepoint | SAVEPOINT |
SAVE TRANSACTION |
(No effect) |
| Release a savepoint | RELEASE SAVEPOINT |
(Not supported) | (No effect) |
| Rollback to savepoint | ROLLBACK WORK TO SAVEPOINT |
ROLLBACK TRANSACTION |
(No effect) |
Syntax Support in SQL Server 2016 and later, and Azure SQL
- NOTE: This does not necessarily include Azure Synapse, SQL Server Parallel Data Warehouse, and Analytics Platform System.
| T-SQL Syntax | Semantics |
|---|---|
| Similar and Ambiguous syntax: | These commands are easily confused for TRANSACTION control statements, but in SQL Server are completely unrelated to TRANSACTIONS. |
BEGIN |
BEGIN, by itself, without any following TRAN or TRANSACTION keyword, denotes a statement block terminated by END. |
START |
Undocumented, but recognized by SSMS, curiously. |
| Start a transaction: | |
BEGIN WORK |
Not supported by SQL Server |
START WORK |
Not supported by SQL Server |
START TRANSACTION |
Not supported by SQL Server |
BEGIN TRAN |
Identical to BEGIN TRANSACTION |
BEGIN TRAN <transaction_name> |
Identical to BEGIN TRANSACTION <transaction_name>. transaction_name can also be a @variable containing the transaction name. Named transactions are mostly used with nested transactions, but committing an inner transaction is a NOOP and only the outermost transaction can be used with ROLLBACK |
BEGIN TRANSACTION |
Starts an unnamed transaction. |
BEGIN TRANSACTION <transaction_name> |
transaction_name can also be a @variable containing the transaction name. |
| Commit a transaction | |
COMMIT |
Identical to COMMIT TRANSACTION |
COMMIT WORK |
Identical to COMMIT TRANSACTION. ISO SQL compliant. |
COMMIT TRAN |
Identical to COMMIT TRANSACTION |
COMMIT TRAN <transaction_name> |
Identical to COMMIT TRANSACTION <transaction_name>. transaction_name can also be a @variable containing the transaction name. |
COMMIT TRANSACTION |
Commits an unnamed transaction started with BEGIN TRANSACTION. Cannot be used to create a savepoint (use SAVE TRANSACTION for that). |
COMMIT TRANSACTION <transaction_name> |
Commits a named transaction started with BEGIN TRANSACTION <transaction_name>. Cannot be used to create a savepoint (use SAVE TRANSACTION <savepoint_name> for that). Committing a nested transaction is a no-op in SQL Server: inner/nested changes are only committed when the outermost transaction is committed. |
| Rollback a transaction | |
ROLLBACK |
Identical to ROLLBACK TRANSACTION. |
ROLLBACK WORK |
Identical to ROLLBACK TRANSACTION. ISO SQL compliant. |
ROLLBACK TRAN |
Identical to ROLLBACK TRANSACTION. |
ROLLBACK TRAN <transaction_name> |
Identical to ROLLBACK TRANSACTION <transaction_name>. transaction_name can also be a @variable containing the transaction name. |
ROLLBACK TRAN <savepoint_name> |
Identical to ROLLBACK TRANSACTION <savepoint_name>. savepoint_name can also be a @variable containing the savepoint name. |
ROLLBACK TRANSACTION |
Rolls back the current explicit or implicit transaction. |
ROLLBACK TRANSACTION <transaction_name> |
Rolls back a named transaction. transaction_name must be the outermost transaction's name when working with nested transactions. |
ROLLBACK TRANSACTION <savepoint_name> |
savepoint_name must have been previously created with SAVE TRANSACTION <savepoint_name> (this is how the ROLLBACK TRANSACTION command for transactions vs. savepoints is disambiguated). |
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dai |
