SQL Server: Script to find the last Executed Query using SPID

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.

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.

Find last executed query using SYS.SYSPROCESSES and SYS.DM_EXEC_SQL_TEXT:

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.

More from dbrnd.com

Leave a Reply

3 Comments on "SQL Server: Script to find the last Executed Query using SPID"

Notify of
Sort by:   newest | oldest | most voted

Please let me now if you’re looking for a writer for your blog.
You have some really good articles and I feel I wold
be a good asset. If you ever want to take some of the
load off, I’d absolutely love to write some material for yoiur blog in exchange for a link bacck tto
mine. Please send me an emaill if interested. Regards!


My brother suggested I might like this web site.
He used to be totally right. This put up truly made my day.
You cann’t imagine simply how much time I had spent for this
blog ! Thanks!