Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql server COMMIT without Tran | Transaction?

According to msdn : the commit syntax is :

enter image description here

However when I omit the tran/transaction words - it does compile and run with NO errors.enter image description here

How can it be working ?

Does it do something else instead ?

like image 538
Royi Namir Avatar asked Feb 13 '12 13:02

Royi Namir


People also ask

What happens if you don't commit a transaction SQL?

As long as you don't COMMIT or ROLLBACK a transaction, it's still "running" and potentially holding locks. If your client (application or user) closes the connection to the database before committing, any still running transactions will be rolled back and terminated.

Can we use ROLLBACK without using commit?

A rollback command can only be executed if the user has not performed the COMMIT command on the current transaction or statement.

Is commit required in SQL Server?

Sql server unlike oracle does not need commits unless you are using transactions. Immediatly after your update statement the table will be commited, don't use the commit command in this scenario.

What is commit Tran in SQL?

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.


1 Answers

The COMMIT in question is not actually COMMIT you think. See COMMIT WORK

COMMIT [ WORK ] [ ; ]

Remarks

This statement functions identically to COMMIT TRANSACTION, except COMMIT TRANSACTION accepts a user-defined transaction name. This COMMIT syntax, with or without specifying the optional keyword WORK, is compatible with SQL-92.

So COMMIT by itself is COMMIT WORK which is identical to COMMIT TRANSACTION.
Ditto for ROLLBACK [ WORK ]

After comment,

BEGIN TRANSACTION gbn
SELECT 1
COMMIT gbn -- fail
GO
BEGIN TRANSACTION gbn
SELECT 2
COMMIT TRAN gbn -- works
GO
like image 135
gbn Avatar answered Oct 06 '22 12:10

gbn