SQL Server- Get Recently executed Queries.

This is Amazing, we can get recently executed Queries in SQL server.

SQL Server saves recent queries in table sys.dm_exec_sql_text.

I didn't know this before.

This is very useful in case we didn't save query before closing SQL editor.

We can get it from this table, Here is Query to get all recent queries

 

SELECT

qrystat.last_execution_time AS ExectuionTime,

qrytext.text AS Query

FROM sys.dm_exec_query_stats qrystat

CROSS APPLY sys.dm_exec_sql_text(qrystat.sql_handle) qrytext

ORDER BY qrystat.last_execution_time DESC

 

Thank You.