We often need to find the last running query or based on SPID need to know which query was executed. SPID is returns sessions ID of the current user process. The acronym SPID comes from the name of its earlier version, Server Process ID.
To know which sessions are running currently, run the following command:
SELECT @@SPID
GO
In our case, we got SPID 57, which means the session that is running this command has ID of 57.
Now, let us open another session and run the same command. Here we get different IDs for different sessions.
In our case, we got SPID 61. Please note here that this ID may or may not be sequential.
In session with SPID 61, we will run any query. In session with SPID 57, we will see which query was run in session with SPID 61.
Let us run a simple SELECT statement in session with SPID 61 and in session with SPID 57 run the following command.
DBCC INPUTBUFFER(61)
GO
Now, here in DBCC command we have passed the SPID of previous session; we will see the text below. The following image illustrates that we get the latest run query in our input buffer.

There are several ways to find out what is the latest run query from system table sys.sysprocesses.
DECLARE @sqltext VARBINARY(128)
SELECT @sqltext = sql_handle
FROM sys.sysprocesses
WHERE spid = 61
SELECT TEXT
FROM sys.dm_exec_sql_text(@sqltext)
GO
The following image portrays that we get the latest run query in our input buffer.

Please note that in any session there may be multiple running queries, but the buffer only saves the last query and that is what we will be able to retrieve.
There is one more way to achieve the same thing – using function fn_get_sql
DECLARE @sqltext VARBINARY(128)
SELECT @sqltext = sql_handle
FROM sys.sysprocesses
WHERE spid = 61
SELECT TEXT
FROM ::fn_get_sql(@sqltext)
GO

All the three methods are same but I always prefer method 2 where I have used sys.sysprocesses.
Today, we have explored a very simple topic. Let me know if you find it useful.
Reference : Pinal Dave (http://blog.sqlauthority.com)




Thanks sir, Nice article.
When we are running big scripts at that time this information
is very useful.
Thanks Kuldip,
I really like that you read almost all the blog posted.
Kind Regards,
Pinal
Note that “fn_get_sql” is going to be removed in a future version of SQL Server, and should not be used in new development.
sys.sysprocesses is for backwards compatiibility only, and for new development BOL recommends using these views instead.
sys.dm_exec_connections
sys.dm_exec_sessions
sys.dm_exec_requests instead
You can view backward compatibility mappings here
http://msdn.microsoft.com/en-us/library/ms187997.aspx
Dear Pinal
I tried it and able to retrive last query which was ran Except current session
Regards
Jayant Das
Hi,
imagine we have a very very long running stored procedure. When I use this method, what I get? It will be the whole procedure or the current running statement from this procedure?
Thanks.
Great today i search for it and find it in your blog…
i wonder how i miss this one well nice one i really need this today..
Thanks.. for this
Thanks Pinal. this is really helpful