See Who Is Blocking Your SQL Server

If you have ever monitored any blocking problems in SQL Server, you know that sp_who only shows you the spid (SQL Server’s internal Process ID) that is causing the blocking for each spid that is blocked. Often a blocked spid is shown as causing blocking for another spid. To see the spid (or spids) that started the whole mess off, execute the following SQL:

  SELECT p.spid,convert(char(12), d.name) db_name, program_name, convert(char(12), l.name) login_name, convert(char(12), hostname) hostname, cmd, p.status, p.blocked, login_time, last_batch, p.spidFROM      master..sysprocesses pJOIN      master..sysdatabases d ON p.dbid =  d.dbidJOIN      master..syslogins l ON p.suid = l.suidWHERE     p.blocked = 0AND       EXISTS (  SELECT 1          FROM      master..sysprocesses p2          WHERE     p2.blocked = p.spid )

We built this into our own version of sp_who, called sp_hywho. See the listing below.

Code for sp_hywho:

 if exists (select * from sysobjects    where id = object_id('dbo.sp_hywho')    and sysstat & 0xf = 4)   drop procedure dbo.sp_hywhoGOCreate Procedure sp_hywho(    @vcDBName sysname = NULL )ASSET NOCOUNT ONIF EXISTS ( SELECT 1   FROM   master..sysprocesses p   WHERE   p.blocked = 0    AND   EXISTS ( SELECT 1      FROM   master..sysprocesses p2      WHERE   p2.blocked = p.spid ) )BEGIN   PRINT "Blocking caused by:"   PRINT ""   SELECT   p.spid   ,convert(char(12), d.name) db_name   , program_name   , convert(char(12), l.name) login_name   , convert(char(12), hostname) hostname   , cmd              , p.status           , p.blocked   , login_time                     , last_batch    , p.spid   FROM   master..sysprocesses p   JOIN   master..sysdatabases d ON p.dbid =  d.dbid    JOIN   master..syslogins l ON p.suid = l.suid    WHERE   p.blocked = 0    AND   EXISTS ( SELECT 1      FROM   master..sysprocesses p2      WHERE   p2.blocked = p.spid )    AND   (p.dbid = DB_ID( @vcDBName ) OR @vcDBName IS NULL)   ORDER BY 2,IsNull(Ltrim(program_name),"ZZZZZZZZZ"),4,5   PRINT ""ENDSELECT   p.spid,convert(char(12), d.name) db_name, program_name, convert(char(12), l.name) login_name, convert(char(12), hostname) hostname, cmd           , p.status        , p.blocked, login_time                  , last_batch , p.spidFROM   master..sysprocesses pJOIN   master..sysdatabases d ON p.dbid =  d.dbid JOIN   master..syslogins l ON p.suid = l.suid WHERE   (p.dbid = DB_ID( @vcDBName ) OR @vcDBName IS NULL)ORDER BY 2,IsNull(Ltrim(program_name),"ZZZZZZZZZ"),4,5SET NOCOUNT OFFGOif exists (select * from sysobjects    where id = object_id('dbo.sp_hywho')    and sysstat & 0xf = 4)   GRANT EXEC ON dbo.sp_hywho TO PUBLICGO
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a