Friday, 20 February 2015

SQL SERVER – Get Latest SQL Query for Sessions – DMV


In recent SQL Training I was asked, how can one figure out what was the last SQL Statement executed in sessions.
The query for this is very simple. It uses two DMVs and created following quick script for the same.
SELECT session_idTEXT
FROM 
sys.dm_exec_connectionsCROSS APPLY sys.dm_exec_sql_text(most_recent_sql_handleAS ST
While working with DMVs if you ever find any DMV has column with name sql_handle you can right away join that DMV with another DMV sys.dm_exec_sql_text and can get the text of the SQL statement.

No comments:

Post a Comment