🧱 Identifying Head Blockers in SQL Server
Many DBAs find it hard to quickly identify what is blocking other processes in SQL Server. Here's a standalone script that helps you identify the head blockers and the sessions they are blocking. It uses sp_who2 , DBCC INPUTBUFFER , and a bit of cursor logic to tie it all together. 🔍 Tip: Run this in SQL Server Management Studio (SSMS) and set the output to "Results in Text" using CTRL+T . SET NOCOUNT ON DECLARE @spid VARCHAR(10) DECLARE @blkby VARCHAR(10) DECLARE @stmt VARCHAR(100) IF NOT EXISTS (SELECT TOP 1 name FROM tempdb..sysobjects WHERE name LIKE '#temp%') BEGIN CREATE TABLE #temp ( spid INT, status VARCHAR(100), login VARCHAR(50), hostname VARCHAR(25), blkby VARCHAR(10), dbname VARCHAR(25), command VARCHAR(100), cputime INT, diskio INT, lastbatch VARCHAR(25), programname VARCHAR(255), spid2 INT ) END ELSE BEGIN TRUNCATE TABLE #temp END INSERT INTO #temp EXEC sp_who2 DECLARE curs CURSOR FOR SELECT CONVERT(VARCHA...