SQL Transactions

A good article on implicit and explicit SQL Transactions:

Implicit transaction mode is not SQL Server’s default. You have to request implicit transaction mode either with a SET statement:

SET IMPLICIT_TRANSACTIONS ON;

That was from Did you Know? Nesting Transactions by Kalen Delaney.

-- Here's a full script to illustrate the behavior of @@trancount with implicit transactions:
SET IMPLICIT_TRANSACTIONS OFF;
GO
IF EXISTS ( SELECT *
 FROM sys.objects
 WHERE NAME = 'T1'
 AND type = 'U' )
 DROP TABLE T1;
GO
CREATE TABLE T1 (col1 INT);
GO
INSERT INTO T1
 SELECT 1;
GO

SET IMPLICIT_TRANSACTIONS ON;
GO
BEGIN TRAN;
 SELECT @@trancount;

UPDATE T1
 SET col1 = col1 + 1;
COMMIT TRAN;
SELECT @@trancount;

COMMIT TRAN;
SELECT @@trancount;
GO
SET IMPLICIT_TRANSACTIONS OFF;
GO

And more, On Transactions, errors and rollbacks from SQL in the Wild by Gail Shaw.

Comments are closed.

Post Navigation