欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

SQL Server 得到SPID,唯一的sessionID

系統 3175 0

? ? ?像.net中的session一樣,假設能知道了數據庫中的sessionID,那全部的操作都能知道了,由于有了這個唯一的身份識別的標識。

? ? ?能夠做的事情有非常多,如:當前哪個用戶在做什么操作,在運行什么sql, 又如一個比較大的邏輯中要分別運行非常多存儲過程,

? ? 在運行這些存儲過程的過程其中,你想知道當前運行的進度,SQLServer正在運行哪個段sql語句,那么通過sessionID是非常easy

? ?就得到這些信息的。

??SQL Server 得到SPID,唯一的sessionID:

? ? SELECT @@SPID

? ? 曾經我一直不知道,近期又裝了SQLServer2014,發現每開一個Query 界面就有一個ID出來。我就特別想知道怎么取sessionID.

以下的存儲過程是用來查看哪些sessionID正在運行什么操作。

create PROC [dbo].[dba_WhatSQLIsExecuting]
AS

BEGIN
-- Do not lock anything, and do not get held up by any locks.
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED


-- What SQL Statements Are Currently Running?
SELECT [Spid] = session_Id
, ecid
, [Database] = DB_NAME(sp.dbid)
, [User] = nt_username
, [Status] = er.status
, [Wait] = wait_type
, [Individual Query] = SUBSTRING (qt.text,
er.statement_start_offset/2,
(CASE WHEN er.statement_end_offset = -1
THEN LEN(CONVERT(NVARCHAR(MAX), qt.text)) * 2
ELSE er.statement_end_offset END -
er.statement_start_offset)/2)
,[Parent Query] = qt.text
, Program = program_name
, Hostname
, nt_domain
, start_time
FROM sys.dm_exec_requests er
INNER JOIN sys.sysprocesses sp ON er.session_id = sp.spid
CROSS APPLY sys.dm_exec_sql_text(er.sql_handle)as qt
WHERE session_Id > 50 -- Ignore system spids.
AND session_Id NOT IN (@@SPID) -- Ignore this current statement.
--and DB_NAME(sp.dbid)='RangeCheckTool'
ORDER BY 1, 2
END

? ? 還能夠參考以下的文章:

? ?http://www.mssqltips.com/sqlservertip/1799/identify-last-statement-run-for-a-specific-sql-server-session/

? ?

Identify last statement run for a specific SQL Server session

Problem
I was reading a recent blog post from? Pinal Dave , SQL Server MVP, regarding? returning information on the latest query executed for a given session .? He offered up a couple options to return the last query statement executed, settling upon querying the sys.sysprocesses system compatibility view, but another way that this can be done is through the Dynamic Management Views and Functions.? The process for doing so is quite straight-forward and works in all versions of Microsoft SQL Server since DMOs (dynamic management objects) were integrated into SQL Server.?

Solution
Before proceeding we should take a second to explain what a session is.? In Microsoft SQL Server, a session is synonymous with a user process.? Previous to SQL 2005 sessions were referred to - and identified solely - as SPIDs (short for session id).? A SPID uniquely identifies a session and a SPID is unique across the SQL Server instance.? In an attempt to conform SQL Server object identifiers to be more user-friendly and to standardize a naming convention across all system objects, sessions are now identified across the DMO and system catalog views as session_id.? You'll see similar changes between previous versions of SQL Server and current versions where all object identifiers are concerned.

You can use the @@spid() system function to return the session_id of the current session as follows:

SELECT? @@SPID

For my test I get session_id = 52.

So, now that we've identified what session_id uniquely identifies the session I'm using during this demonstration, I'll do a simple query against the Northwind database.

SELECT? C.[CompanyName]??
FROM? [Northwind].dbo.[Customers]?C??
WHERE? C.[City]? =? 'Berlin'??
ORDER?BY? [C].[CompanyName]

At this point I'll now open up a separate query window in SQL Server Management Studio.? If I now execute?the first query above you'll see that this registers as a new session on the SQL Server instance:

SELECT? @@SPID

For my test I get session_id = 53

Now I can utilize the? sys.dm_exec_connections ?Dynamic Management View, in conjunction with the sys.dm_exec_sql_text ?Dynamic Management Function to return the last query statement executed against the SQL Server instance on a selected session.? In all truth, you can return the last query executed on all sessions, but for the sake of this discussion we're limiting the results based upon the session_id (52) we've identified above.? I'll present the query, then we can examine in detail what it provides for us.

SELECT? DEST. TEXT??
FROM?
sys.[dm_exec_connections]?SDEC?
? CROSS? APPLY?sys.[dm_exec_sql_text] ( SDEC.[most_recent_sql_handle] )? AS? DEST?
WHERE? SDEC.[most_recent_session_id]? =? 52?

The output for this query shows the statement that was run for session_id 52.

So what just happened?? Simply-put, we returned the results from the sys.dm_exec_connections DMV, limiting the results by the session_id (52) we identified above.? We, submitted the value contained in the most_recent_sql_handle column of this DMV to the sys.dm_exec_sql_text Dynamic Management Function.? That function then returned as text, the value of the sql_handle we passed to it.?

So what is a sql_handle?? Think of a sql_handle as a unique identifier for a query that is unique across the entire SQL Server instance.? Just as a session_id uniquely identifies a session, so does a sql_handle identify a query.? The actual value of the sql_handle column is very cryptic.? The value for the most_recent_sql_handle in this example is shown below:

SELECT? SDEC.[most_recent_sql_handle] ,? DEST.[text]??
FROM? sys.[dm_exec_connections]?SDEC?
? CROSS? APPLY?sys.[dm_exec_sql_text] ( SDEC.[most_recent_sql_handle] )? AS? DEST?
WHERE? SDEC.[most_recent_session_id]? =? 52?

Here we can see the value of the sql_handle and the text translation.

The handle itself does not really do much for us without the function call that rationalizes it into the original query text.? As you can see though, this very simple query does provide us with yet another option for returning information on what users are (or have been) doing on the SQL Server instances we support.

Next Steps

  • The Dynamic Management Objects have so much to offer the DBA.?? Check out other tips on DMOs ?from MSSQLTips.com.
  • Read more tips by the author? here .
  • Still interested in information on sysprocesses, whether as a system table (pre-SQL 2005) or system view?? Here are some? tips ?that meet your needs.

?

SQL Server 得到SPID,唯一的sessionID


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 91成人 | 欧美性猛交一区二区三区精品 | 久草在线观看首页 | 国产一区二区在线看 | av电影网站在线观看 | jizzjizzjizz亚洲18| 男女做性无遮挡免费视频 | 中文在线一区二区 | 亚洲乱人伦在线 | 日韩在线观看视频一区二区三区 | 青青草娱乐视频 | 国产亚洲精品一品区99热 | 午夜影皖普通区 | 五月缴情 | 一区二区免费视频观看 | 天堂av资源 | 日日麻批免费视频 | 日韩欧美国产一区二区 | 欧美视频在线一区 | 成人亚洲一区二区色情无码潘金莲 | 波多野结衣 久久 | 王骏迪的个人资料 | 99热热热 | 免费日本在线视频 | 暴操美女视频 | 成人欧美一区二区三区在线播放 | 国产二区三区毛片 | 久久精品夜夜夜夜夜久久 | 久久人精品 | 日本在线播放不卡一区二区三区 | 国产欧美在线视频 | 欧美日韩综合在线视频免费看 | 午夜在线观看视频 | 久久免费看少妇高潮A片麻豆 | 超级在线牛碰碰视频 | 久久曰 | 天天操天天舔天天干 | 91精品观看| 成人在线免费观看 | 亚洲精品国产第1页 | 亚洲日本免费 |