@@ TRANCOUNT and current connection

Suppose I connect to SQL Server 2008 through SQL Server Management Studio (SSMS) and open a new window W1

by clicking the New Query tab and writing the following inside W1:

BEGIN TRANSACTION;

      

If I execute this statement 5 times and then I write (inside W1

)

SELECT @@TRANCOUNT;

      

then the return value will be 5. But if I open another window W2

(inside the same SSMS instance and hence on the same connection) and write inside W2

SELECT @@TRANCOUNT; 

      

then the return value will be 0.

@@ TRANCOUNT returns the number of active transactions for the current connection.

Both windows W1 and W2 were open on the same connection, so shouldnt (as per the quote above) both in W1

and in W2

variable @@ TRANCOUNT keep the same value?

thanks

+2


a source to share


1 answer


Each query window in SSMS is a separate connection running on a separate spid. The number in brackets on the query window tab is your connection number for the current window.



When opening multiple windows and open transactions in each case, if you like, you should be able to see how each one appears in the results of calling sp_who2. Using the speed number from the window tab, you should be able to find each line in the results of this process. You can also find detailed information about your connection in the status bar at the bottom of each query window, as well as in the properties window for each open query window.

+5


a source







All Articles