I saw in SQL Server about the transaction part it has following statements:
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?
BEGIN WORK, instead use BEGIN TRANSACTION (which also supports named transactions).
BEGIN (by itself) denotes a statement-block and is completely unrelated to transactions.BEGIN WORK, SQL Server does support COMMIT WORK and ROLLBACK WORK.
COMMIT WORK has identical semantics to COMMIT TRANSACTION.ROLLBACK WORK has identical semantics to ROLLBACK TRANSACTION.START TRANSACTION with COMMIT WORK and ROLLBACK WORK which is a strangely inconsistent use of language keywords.
START WORK though SSMS does not recognize the syntax.| 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) |
| 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). |
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With