Categories
Sql Server

How to Find Recently Executed Queries in SQL Server Database

How to Find Recently Executed Queries in SQL Server Database

In this tutorial we will learn How to Find Recently Executed Queries in SQL Server Database. Below mentioned query will fetch query time and query that has been executed. For this we are using Dynamic Management Views. This is very helpful, when we have to find recently executed queries on database server for auditing purpose.

Earlier we had learnt about SQL Server Schema OwnershipAlwaysOn Availability Groups SQL ServerHow To Set Up Database Mirroring In Windows WorkgroupSQL Server Database Mirroring in Domain Environment and SQL Server DBA Interview Question Accenture.

Query:

SELECT 
eqrystats.last_execution_time AS [Execution Time],
esqltxt.TEXT AS [Executed Query]
FROM
sys.dm_exec_query_stats AS eqrystats
CROSS APPLY sys.dm_exec_sql_text(eqrystats.sql_handle) AS esqltxt
ORDER BY eqrystats.last_execution_time DESC

Result:

How to Find Recently Executed Queries in SQL Server Database