Getting Started with ASP.NET MVC - Part 6: ASP.NET MVC and Entity Framework
First Time? You can support us by signing up. It takes only 5 seconds. Click here to sign up. If you already have an account, click here to login.


Upload Image Close it
Select File

Browse by Tags · View All
sql_server 217
t-sql 211
tsql 113
sqlserver 94
BRH 78
#SQLServer 65
#TSQL 55
SQL Server 32
function 11
SSMS 9

Archive · View All
August 2007 17
August 2010 8
June 2011 7
November 2007 7
November 2011 6
August 2011 6
October 2011 6
July 2011 6
September 2011 6
December 2011 6

Madhivanan's TSQL Blog

@@TRANCOUNT is always incremented in a trigger

Jan 9 2012 1:24AM by Madhivanan   

The system variable @@TRANCOUNT is used to get the number of transactions that are active. Consider the following code

begin transaction
select 3
select @@TRANCOUNT 
commit transaction

The result of @@TRANCOUNT is 1. Consider the following code.

begin transaction
select 3
begin transaction
select 456
select @@TRANCOUNT 
commit transaction
commit transaction


The result of @@TRANCOUNT is 2.

So whenever there is a transaction the variable @@TRANCOUNT gets incrementedx by 1. In an Auto commit mode if you dont use a transaction, @@TRANCOUNT will never gets incremented. But this variable gets incremented always in a trigger.

This is what BOL specifies

@@TRANCOUNT is incremented by one when entering a trigger, even when in autocommit mode. (The system treats a trigger as an implied nested transaction.)

So you need to be aware of this and use @@TRANCOUNT effectively.

Tags: t-sql, sql_server, sqlserver, tsql, SQL Server, #SQL SERVER, @@Trancount,


Madhivanan
4 · 39% · 8850
2
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

1  Comments  

  • I had learnt this one the hard way, so thank-you for posting it, Madhivanan!

    commented on Jan 28 2012 4:49AM  .  Report Abuse This post is not formatted correctly
    Nakul Vachhrajani
    6 · 26% · 5941

Your Comment


Sign Up or Login to post a comment.

"@@TRANCOUNT is always incremented in a trigger" rated 5 out of 5 by 2 readers
@@TRANCOUNT is always incremented in a trigger , 5.0 out of 5 based on 2 ratings
    Copyright © Beyondrelational.com Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising