--查阻塞--
SELECT t1.resource_type                               AS [锁类型],
       Db_name(resource_database_id)                  AS [数据库名],
       t1.resource_associated_entity_id               AS [阻塞资源对象],
       t1.resource_description                        AS [资源描述信息],
       t1.request_mode                                AS [请求的锁],
       t1.request_session_id                          AS [等待会话],
       t2.wait_duration_ms                            AS [等待时间],
       (SELECT [text]
        FROM   sys.dm_exec_requests AS r WITH (NOLOCK)
               CROSS APPLY sys.Dm_exec_sql_text(r.[sql_handle])
        WHERE  r.session_id = t1.request_session_id)  AS [等待会话执行的批SQL],
       (SELECT Substring(qt.[text], r.statement_start_offset / 2, ( CASE
                                                                      WHEN r.statement_end_offset = -1 THEN Len(CONVERT(NVARCHAR(max), qt.[text])) * 2
                                                                      ELSE r.statement_end_offset
                                                                    END ) / 2)
        FROM   sys.dm_exec_requests AS r WITH (NOLOCK)
               CROSS APPLY sys.Dm_exec_sql_text(r.[sql_handle]) AS qt
        WHERE  r.session_id = t1.request_session_id)  AS [等待会话执行的SQL],
       t2.blocking_session_id                         AS [阻塞会话],
       (SELECT c.client_net_address
        FROM   sys.dm_exec_connections AS c WITH (NOLOCK)
        WHERE  c.session_id = t2.blocking_session_id) AS [阻塞会话客户端IP],
       (SELECT s.program_name
        FROM   sys.dm_exec_sessions AS s WITH (NOLOCK)
        WHERE  s.session_id = t2.blocking_session_id) AS [阻塞会话连接方式],
       (SELECT c.connect_time
        FROM   sys.dm_exec_connections AS c WITH (NOLOCK)
        WHERE  c.session_id = t2.blocking_session_id) AS [阻塞会话连接创建时间],
       (SELECT [text]
        FROM   sys.sysprocesses AS p
               CROSS APPLY sys.Dm_exec_sql_text(p.[sql_handle])
        WHERE  p.spid = t2.blocking_session_id)       AS [阻塞会话执行的批SQL],
       Getdate()
FROM   sys.dm_tran_locks AS t1 WITH (NOLOCK)
       INNER JOIN sys.dm_os_waiting_tasks AS t2 WITH (NOLOCK)
               ON t1.lock_owner_address = t2.resource_address