Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / SQL

sp_who2 to find dead locks in SQL Server

4.88/5 (8 votes)
8 Jan 2015CPOL 86.9K  
Find deadlocks in SQL Server using sp_who2 (an undocumented and thus unsupported stored procedure)

Introduction

sp_who2 is a undocumented thus unsupported stroed procedure in SQL server, but widely used inststed of sp_who to list processes currently active in SQL Server. Both these procudures are designed to retrive same result set, however sp_who2 adds some extra columns which sp_who does not include. Furthermore, sp_who2 makes an effort to make the display to be as compact as possible for output in text mode.

sp_who2 usually helps me to track / kill deadlocks in my database.

Using the code

The result set of sp_who2 will contains a column named BlkBy, this represents the SPID that is currently stopping the SPID in the row.

Sometimes many rows will show SPID numbers in the BlkBy column.

This is because there is a chain of blockers. The way this occurs usually starts with one “lead blocker” blocking another process.=

C++
//
// sp_who2
//

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)