@@TRANCOUNT 和当前连接

2021-09-10 00:00:00 sql tsql sql-server-2008 sql-server

假设我通过 SQL Server Management Studio ( SSMS ) 连接到 SQL Server 2008 并通过单击新建查询"选项卡打开新窗口 W1 并在 W1 中写入以下内容:

Assume I connect to SQL server 2008 via SQL Server Management Studio ( SSMS ) and open new window W1 by clicking on New Query tab and write the following inside W1:

BEGIN TRANSACTION;

如果我执行这个语句5次,然后写(在W1里面)

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

SELECT @@TRANCOUNT;

,那么返回的值将是 5.但是如果我打开另一个窗口 W2(在同一个 SSMS 实例内,因此在同一个连接上)并在 W2 内写入

, then the value returned will be 5. But if I open another window W2 ( inside the same SSMS instance and thus on the same connection ) and write inside W2

SELECT @@TRANCOUNT; 

那么返回的值为 0.

@@TRANCOUNT 变量返回当前连接的活动事务数.

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

W1 和 W2 窗口都在同一个连接上打开,所以不应该(根据上面的引用)在 W1W2 变量 @@TRANCOUNT 中保持相同的价值?

Both W1 and W2 windows were opened on the same connection, so shouldn’t ( according to the above quote ) in both W1 and W2 variable @@TRANCOUNT hold the same value?

谢谢

推荐答案

SSMS 中的每个查询窗口都是一个单独连接,在单独的 spid 上运行.查询窗口选项卡中括号中的数字是您当前窗口的连接号.

Every query window in SSMS is a separate connection, running on a separate spid. The number in parenthesis in the query window tab is your connection number for the current window.

打开几个窗口,并根据需要在每个窗口中打开事务,您应该能够看到每个窗口都显示在调用 sp_who2 的结果中.使用窗口选项卡中的 spid 编号,您将能够在该过程的结果中找到每一行.您还可以在每个查询窗口底部的状态栏中以及每个打开的查询窗口的属性窗口中找到有关您的连接的详细信息.

With a couple windows open, and open transactions in each if you like, you should be able to see each one of them show up in the results of a call to sp_who2. Using the spid number from the window tab you'll be able to find each row in the results of that proc. You can also find detailed information about your connection in the status bar at the bottom of each query window, and also in the properties window for each open query window.

相关文章