SQL SERVER – Get Latest SQL Query for Sessions – DMV

Posted by pinaldave on SQL Authority See other posts from SQL Authority or by pinaldave
Published on Wed, 05 May 2010 01:30:01 +0000 Indexed on 2010/05/05 1:39 UTC
Read the original article Hit count: 1012

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_id, TEXT
FROM
sys.dm_exec_connections
CROSS APPLY sys.dm_exec_sql_text(most_recent_sql_handle) AS 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.

Reference: Pinal Dave (http://blog.SQLAuthority.com)


Filed under: Pinal Dave, SQL, SQL Authority, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology Tagged: DMV, SQL DMV

© SQL Authority or respective owner

Related posts about Pinal Dave

Related posts about sql