Sql server 查询造成死锁的语句

select a.request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableName,t2.SQL语句
from sys.dm_tran_locks a
left join (
SELECT
    r.session_id,
    r.command,
    r.status,
    r.wait_type,
    r.wait_time,
    r.last_wait_type,
    t.resource_type,
    t.resource_description,
    t.request_mode,
    t.request_session_id,
    s.text AS 'SQL语句'
FROM sys.dm_exec_requests AS r
JOIN sys.dm_tran_locks AS t ON r.session_id = t.request_session_id
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) AS s
left join (select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableName
from sys.dm_tran_locks where resource_type='OBJECT') a on a.spid=r.session_id
) t2 on t2.request_session_id=a.request_session_id

where a.resource_type='OBJECT' and t2.request_session_id is not null

 

posted @ 2023-07-27 16:06  曲琦  阅读(911)  评论(0编辑  收藏  举报