dataiotech Labs
Scripts Library/sqlserver/#41
Script #41
sqlserver
Lockingread-onlyMin Version: 2012

SQL Server Running Requests and Blocking Chain

Shows active requests, wait types, blocking sessions and the running statement together. The first diagnostic query to run during a slowdown.

#dmv#blocking#sessions#incident
sqlserver · sql
SELECT r.session_id, r.blocking_session_id, r.status, r.command,
       r.wait_type, r.wait_time AS wait_ms,
       r.cpu_time, r.logical_reads,
       DB_NAME(r.database_id) AS db_name,
       s.login_name, s.host_name,
       SUBSTRING(t.text, (r.statement_start_offset/2)+1,
         ((CASE r.statement_end_offset WHEN -1 THEN DATALENGTH(t.text)
           ELSE r.statement_end_offset END - r.statement_start_offset)/2)+1) AS running_statement
FROM sys.dm_exec_requests r
JOIN sys.dm_exec_sessions s ON s.session_id = r.session_id
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) t
WHERE r.session_id <> @@SPID
  AND s.is_user_process = 1
ORDER BY r.blocking_session_id DESC, r.wait_time DESC;
Operational Notes & Remediation Guidance

Verify in a staging environment prior to running in production. This is a read-only diagnostic query; it does not acquire exclusive locks.

Production Safety & Execution Standards
  • This script is designed for read-only diagnostics; it does not alter schema or modify data directly.
  • Prior to executing on production systems under high load, verify the target database version (Min. Version: 2012).
  • Exercise caution with mutating actions (e.g., terminating sessions or dropping indexes) referenced in the notes section.

Engineering Triad: Related Runbooks & Incident Response

Which production incidents and emergency drills execute this script as a diagnosis step?

Runbook Library (63)
Related Alerts & Metrics
M28SQL Server Blocked ProcessesSQL Server
M29SQL Server Deadlock RateSQL Server