Sep 12, 2017

How to find recent executed query in SQL Server

Sometimes we do R&D on query and try to found the solution of any problem and due to some reason you shut down your system without saving query and next you need to recover your R&D query, so using following query you can recover your executed query..


SELECT dest.TEXT AS [MyQuery], a.last_execution_time AS [DateTime]
FROM sys.dm_exec_query_stats AS a
CROSS APPLY sys.dm_exec_sql_text(a.sql_handle) AS dest
WHERE  a.last_execution_time>dateadd(day,-1,getdate())
ORDER BY a.last_execution_time DESC