In this post, I am providing a different method to find last executed query by SPID of SQL Server.
When DBA monitoring performance or serving any request for performance issue, the DBA may be required to find last executed query of a particular client.
You can find SPID using below commands.
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 and this command require the SPID as parameter.
DBCC INPUTBUFFER (SPID)
DECLARE @sqlhandle VARBINARY(64)
SELECT @sqlhandle = sql_handle from sys.sysprocesses where spid = 987
SELECT text FROM sys.dm_exec_sql_text(@sqlhandle)
Please share your ideas and opinions about this topic with me, your contribution will add true value to this topic.
If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.
If you like this post, then please share it with others.
Please follow dbrnd.com, I will share my experience towards the success of Database Research and Development Activity.
I put up a post every day, please keep reading and learning.
Discover Yourself, Happy Blogging !
Anvesh M. Patel.