Pages

Social Icons

Monday 30 April 2012

Identify last statement run for a specific SQL Server session


You can use the @@spid() system function to return the session_id of the current session as follows:


SELECT @@spid


For my test I get session_id = 61


Now run your query:
SELECT * FROM <table> 


Run the following query you will get the your last executed query :


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] = 61 


OR
  
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] = 61 

No comments:

Post a Comment