sql server 查看正在运行的sql 语句
时间: 2023-09-05 16:07:32 浏览: 186
要查看正在运行的SQL语句,可以使用sys.dm_exec_requests视图和sys.dm_exec_sql_text函数。以下是一个示例查询:
SELECT
\[Spid\] = session_Id,
ecid,
\[Database\] = DB_NAME(sp.dbid),
\[User\] = nt_username,
\[Status\] = er.status,
\[Wait\] = wait_type,
\[Individual Query\] = SUBSTRING(qt.text, er.statement_start_offset / 2, (CASE WHEN er.statement_end_offset = -1 THEN LEN(CONVERT(NVARCHAR(MAX), qt.text)) * 2 ELSE er.statement_end_offset END - er.statement_start_offset) / 2),
\[Parent Query\] = qt.text,
Program = program_name,
Hostname,
nt_domain,
start_time
FROM sys.dm_exec_requests er
INNER JOIN sys.sysprocesses sp ON er.session_id = sp.spid
CROSS APPLY sys.dm_exec_sql_text(er.sql_handle) AS qt
WHERE session_Id > 50 -- 忽略系统进程
AND session_Id NOT IN (@@SPID)
这个查询将返回正在运行的SQL语句的相关信息,包括会话ID、数据库、用户、状态、等待类型、单个查询、父查询、程序、主机名、域名和开始时间。
#### 引用[.reference_title]
- *1* [SqlServer——查看当前正在执行SQL语句的sql 并结束该进程](https://blog.csdn.net/qq_37233070/article/details/121855977)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* *3* [SQL server 查询语句](https://blog.csdn.net/wangyuxiang946/article/details/118398986)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文