Recent Queries Completed for a Specific Database

I know how to get the latest executed queries using the following SQL in SSMS -

SELECT 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 

But I want to find them for a specific database. I do not want to use SQL Profiler if I do not need it. Plus, I donโ€™t think that SQL Profiler will allow me to view queries that have already been executed without enabling profiling. I need to do this from SSMS.

+42
sql-server sql-server-2012
Nov 30
source share
1 answer

This works for me to find queries in any database in the instance. I am the system administrator of the instance (check your privileges):

 SELECT deqs.last_execution_time AS [Time], dest.text AS [Query], dest.* FROM sys.dm_exec_query_stats AS deqs CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest WHERE dest.dbid = DB_ID('msdb') ORDER BY deqs.last_execution_time DESC 

This is the same answer as Aaron Bertrand, but he was not placed in the answer.

+53
May 16 '13 at 18:27
source share
โ€” -



All Articles