This article is half-done without your Comment! *** Please share your thoughts via Comment ***
In this post, I am sharing different methods to find last executed query by SPID of SQL Server.
When DBA monitoring the server performance or serving any request on the performance issue, the DBA may be required to find last executed query of a particular client.
You can find SPID using below commands:
1 2 |
SELECT @@SPID --returns current sessions ID SP_WHO --list of all sessions with SPID |
Find last executed query using DBCC INPUTBUFFER:
You can use DBCC INPUTBUFFER to get the last SQL statement issued by a client by passing the SPID as parameter.
1 |
DBCC INPUTBUFFER (SPID) |
Find last executed query using SYS.SYSPROCESSES and SYS.DM_EXEC_SQL_TEXT:
1 2 3 |
DECLARE @sqlhandle VARBINARY(64) SELECT @sqlhandle = sql_handle from sys.sysprocesses where spid = 987 SELECT text FROM sys.dm_exec_sql_text(@sqlhandle) |