問題描述
假設(shè)我通過 SQL Server Management Studio ( SSMS ) 連接到 SQL Server 2008 并通過單擊新建查詢"選項(xiàng)卡打開新窗口 W1
并在 W1 中寫入以下內(nèi)容:
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;
如果我執(zhí)行這個(gè)語句5次,然后寫(在W1
里面)
If I execute this statement 5 times, and then write (inside W1
)
SELECT @@TRANCOUNT;
,那么返回的值將是 5.但是如果我打開另一個(gè)窗口 W2
(在同一個(gè) SSMS 實(shí)例內(nèi),因此在同一個(gè)連接上)并在 W2 內(nèi)寫入
, 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 變量返回當(dāng)前連接的活動事務(wù)數(shù).
@@TRANCOUNT variable returns the number of active transactions for the current connection.
W1 和 W2 窗口都在同一個(gè)連接上打開,所以不應(yīng)該(根據(jù)上面的引用)在 W1
和 W2
變量 @@TRANCOUNT 中保持相同的價(jià)值?
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 中的每個(gè)查詢窗口都是一個(gè)單獨(dú)連接,在單獨(dú)的 spid 上運(yùn)行.查詢窗口選項(xiàng)卡中括號中的數(shù)字是您當(dāng)前窗口的連接號.
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.
打開幾個(gè)窗口,并根據(jù)需要在每個(gè)窗口中打開事務(wù),您應(yīng)該能夠看到每個(gè)窗口都顯示在調(diào)用 sp_who2 的結(jié)果中.使用窗口選項(xiàng)卡中的 spid 編號,您將能夠在該過程的結(jié)果中找到每一行.您還可以在每個(gè)查詢窗口底部的狀態(tài)欄中以及每個(gè)打開的查詢窗口的屬性窗口中找到有關(guān)您的連接的詳細(xì)信息.
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.
這篇關(guān)于@@TRANCOUNT 和當(dāng)前連接的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!