对特定数据库的最后执行的查询

时间:2021-12-14 21:50:22

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

我知道如何在SSMS -中使用以下SQL获取最后执行的查询

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 don't want to use SQL Profiler, if I don't have to. Plus I don't think SQL Profiler will allow me to view queries that were already run without profiling turned on. I need to do this from SSMS.

但我想找一个特定的数据库。如果没有必要,我不想使用SQL分析器。另外,我认为SQL Profiler不允许我查看已经运行的查询而不打开分析。我需要从SSMS做这个。

1 个解决方案

#1


63  

This works for me to find queries on any database in the instance. I'm sysadmin on 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 that Aaron Bertrand provided but it wasn't placed in an answer.

这和亚伦·贝特朗提供的答案是一样的,但并没有给出答案。

#1


63  

This works for me to find queries on any database in the instance. I'm sysadmin on 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 that Aaron Bertrand provided but it wasn't placed in an answer.

这和亚伦·贝特朗提供的答案是一样的,但并没有给出答案。