Retrieve the most recently run queries on a SQL Server

Useful way of finding out what T-SQL has been run on a SQL Server ordered by latest query. This has been tested in both SQL Server 2005 & 2008

SELECT top 1000
deqs.last_execution_time AS [Time], dest.TEXT AS [Query]
FROM sys.dm_exec_query_stats AS deqs
CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest
ORDER BY deqs.last_execution_time DESC

This is better than using sys.fn_get_sql() or dbcc_inputbuffer() as both will be deprecated in future versions of SQL Server

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: