Pages

Monday, April 27, 2015

SQLServer Auto Commit and Implicit Transactions

==============================
Auto Commit and Implicit Transactions
==============================
Auto Commit mode
SQL Server immediately commits the change after executing the statement.

Implicit Transactions Mode
Need to manually control the Rollback and the Commit operation. 
In this mode a new transaction automatically begins after the commit/Rollback. No need to specify BEGIN TRANSACTION.

Explicit Mode
Same as Implicit Transactions Mode, only we begin each transaction with BEGIN TRANSACTION statement.

Turning ON/OFF the implicit transactions mode.
You can turn auto commit ON by setting implicit_transactions OFF:

SET IMPLICIT_TRANSACTIONS OFF
In Management Studio: Tools-> Options -> Query Execution -> SQL Server -> ANSI -> uncheck SET IMPLICIT TRANSACTIONS checkbox

SET IMPLICIT_TRANSACTIONS ON
When the setting is ON, it returns to implicit transaction mode. 
In implicit transaction mode, each transaction must be manually commited or rolled back.

Auto commit is the default for SQL Server 2000 and up.

For example:
SET IMPLICIT_TRANSACTIONS ON
UPDATE MY_TABLE SET col_a =  'A' WHERE col_b = 2
COMMIT TRANSACTION

SET IMPLICIT_TRANSACTIONS ON
UPDATE MY_TABLE SET col_a =  'A' WHERE col_b = 2
ROLLBACK TRANSACTION


No comments:

Post a Comment