日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

SQL Server 得到SPID,唯一的sessionID

發(fā)布時(shí)間:2023/12/13 综合教程 29 生活家
生活随笔 收集整理的這篇文章主要介紹了 SQL Server 得到SPID,唯一的sessionID 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

像.net中的session一樣,假設(shè)能知道了數(shù)據(jù)庫中的sessionID,那全部的操作都能知道了,由于有了這個(gè)唯一的身份識(shí)別的標(biāo)識(shí)。

能夠做的事情有非常多,如:當(dāng)前哪個(gè)用戶在做什么操作,在運(yùn)行什么sql, 又如一個(gè)比較大的邏輯中要分別運(yùn)行非常多存儲(chǔ)過程,

在運(yùn)行這些存儲(chǔ)過程的過程其中,你想知道當(dāng)前運(yùn)行的進(jìn)度,SQLServer正在運(yùn)行哪個(gè)段sql語句,那么通過sessionID是非常easy

就得到這些信息的。

SQL Server 得到SPID,唯一的sessionID:

SELECT @@SPID

曾經(jīng)我一直不知道,近期又裝了SQLServer2014,發(fā)現(xiàn)每開一個(gè)Query 界面就有一個(gè)ID出來。我就特別想知道怎么取sessionID.

以下的存儲(chǔ)過程是用來查看哪些sessionID正在運(yùn)行什么操作。

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 fromPinal Dave, SQL Server MVP, regardingreturning
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.

SELECTC.[CompanyName]
FROM[Northwind].dbo.[Customers]C
WHEREC.[City]='Berlin'
ORDERBY[C].[CompanyName]

At this point I'll now open up a separate query window in SQL Server Management Studio. If I now executethe 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 thesys.dm_exec_connectionsDynamic Management View, in conjunction
with thesys.dm_exec_sql_textDynamic 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.

SELECTDEST.TEXT
FROMsys.[dm_exec_connections]SDEC
CROSSAPPLYsys.[dm_exec_sql_text](SDEC.[most_recent_sql_handle])ASDEST
WHERESDEC.[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:

SELECTSDEC.[most_recent_sql_handle],DEST.[text]
FROMsys.[dm_exec_connections]SDEC
CROSSAPPLYsys.[dm_exec_sql_text](SDEC.[most_recent_sql_handle])ASDEST
WHERESDEC.[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 DMOsfrom MSSQLTips.com.Read more tips by the authorhere.Still interested in information on sysprocesses, whether as a system table (pre-SQL 2005) or system view? Here are sometipsthat
meet your needs.

總結(jié)

以上是生活随笔為你收集整理的SQL Server 得到SPID,唯一的sessionID的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。