@@TRANCOUNT 和当前连接

时间:2022-12-31
本文介绍了@@TRANCOUNT 和当前连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

假设我通过 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.

这篇关于@@TRANCOUNT 和当前连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:TSQL 返回存在的内容 下一篇:SQL Server 用户定义函数来计算年龄段

相关文章

最新文章