問題描述
假設我通過 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 窗口都在同一個連接上打開,所以不應該(根據上面的引用)在 W1
和 W2
變量 @@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 和當前連接的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!