Like the session in. net, if you know the sessionID in the database, all operations will be known, because you have the unique identity.
There are many things that can be done, such as: which user is doing what, what SQL is being executed, and how many stored procedures should be executed separately in a relatively large logic,
In the process of executing these stored procedures, you want to know the progress of the current execution. Which SQL statement is being executed by SQLServer, it is easy to use sessionID
The information is obtained.
SQL Server obtains the SPID, the unique sessionID:
SELECT @ SPID
I have never known before. I recently installed SQLServer2014 and found that each Query interface has an ID. I especially want to know how to get the sessionID.
The following stored procedure is used to view which sessionids are performing what operations.
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
You can also refer to the following article:
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 shoshould 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 authentication ss the SQL Server instance. in an attempt to conform SQL Server object identifiers to be more user-friendly and to standardize a naming convention internal SS all system objects, sessions are now identified into ss 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:
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:
For my test I get session_id = 53
Now I can utilize the sys. dm_exec_connections Dynamic Management View, in conjunction with thesys. 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 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 processing ss 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.