上次对特定数据库执行的查询 [英] Last executed queries for a specific database

查看:23
本文介绍了上次对特定数据库执行的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know how to get the last 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

但我想为特定的数据库找到它们.如果不需要,我不想使用 SQL Profiler.另外,我认为 SQL Profiler 不会允许我查看已运行的查询而未打开分析.我需要通过 SSMS 执行此操作.

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.

推荐答案

这适用于我在实例中查找对任何数据库的查询.我是实例的系统管理员(检查您的权限):

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

这与 Aaron Bertrand 提供的答案相同,但没有放在答案中.

This is the same answer that Aaron Bertrand provided but it wasn't placed in an answer.

这篇关于上次对特定数据库执行的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆