Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)


By Sean Smith,

Over the years (and various versions of SQL Server), I have always had a need for different bits of information from the system tables to view activity on a SQL instance. The Microsoft "sp_who / sp_who2" procedures have always provided good information, but never really everything that I needed (and, more importantly, how I needed it... hence the reason for building my own version).

It can be run as is (example: EXECUTE dbo.usp_who5) or with optional input filter parameters:

  • @Filter: Limit the result set by passing one or more values listed below (can be combined in any order)
  • A - Active sessions only
  • B - Blocked sessions only
  • C - Exclude "SQL_Statement_Batch", "SQL_Statement_Current", "Batch_Pct", and "Query_Plan_XML" columns from the output (less resource-intensive)
  • X - Exclude system reserved SPIDs (1-50)
  • @SPID: Limit the result set to a specific session
  • @Login: Limit the result set to a specific Windows user name (if populated, otherwise by SQL Server login name)
  • @Database: Limit the result set to a specific database
  • @SQL_Text : Limit the result set to SQL statements containing specific text (ignored when "@Filter" parameter contains "C")

When using the procedure you will notice that the query output contains a lot of handy information:

  • SPECID: System Process ID with Execution Context ID
  • Blocked: Blocking indicator (includes type of block and blocking SPID)
  • Running: Indicates if the session is executing (X), waiting (*), inactive (blank), has open transactions (•), or is a background task (--)
  • Login_ID: Windows user name (or login name if user name is unavailable)
  • Login_Name: Full name of the user associated to the "Login_ID" (if available)
  • Elapsed_Time: Elapsed time since the request began (DAYS HH:MM:SS)
  • CPU_Total: CPU time used since login (DAYS HH:MM:SS)
  • CPU_Current: CPU time used for the current process (DAYS HH:MM:SS)
  • Logical_Reads: Logical reads performed by the current process
  • Physical_Reads: Physical reads performed by the current process
  • Writes: Writes performed by the current process
  • Pages_Used: Pages in the procedure cache allocated to the process
  • Nesting_Level: Nesting level of the statement currently executing
  • Open_Trans: Open transactions for the process
  • Last_Row_Count: Row count produced by the last statement executed in the batch
  • Wait_Time: Current wait time (DAYS HH:MM:SS)
  • Wait_Type: Current wait type
  • Last_Wait_Type: Previous wait type
  • Status: Status of the current process
  • Command: Command currently being executed
  • Isolation_Level: Isolation level of the session
  • SQL_Statement_Batch: Batch statement of the session
  • SQL_Statement_Current: Current statement of the session
  • Batch_Pct: Percentage of the batch which has been processed
  • End_Of_Batch: Indicates if the current statement is the last of the entire batch
  • Command_Pct: Percentage of work completed (applicable to a limited set of commands)
  • Command_Time_Left: Time left before the command completes (DAYS HH:MM:SS)
  • Command_Completion: Estimated completion time for the command
  • Previous_Error: Previous error returned by the session
  • Lock_Details: Lock details of the session (in XML format)
  • Lock_Timeout_Seconds: Lock timeout of the session
  • Deadlock_Priority: Deadlock priority of the session
  • Plan_Handle: Identifier for the in-memory plan
  • Query_Plan_XML: Execution plan of the session (in XML format)
  • Plan_Cache_Object_Type: Displays which mechanism is being used for the cached plan (used in conjunction with "Plan_Object_Type")
  • Plan_Object_Type: Displays which mechanism is being used for the cached plan (used in conjunction with "Plan_Cache_Object_Type")
  • Plan_Times_Used: Number of times the plan has been utilized since its creation
  • Plan_Size_MB: Size consumed by the plan in megabytes
  • Since_SPID_Login: Elapsed time since the client logged in (DAYS HH:MM:SS)
  • Since_Last_Batch: Elapsed time since the client last completed a batch request (DAYS HH:MM:SS)
  • Workstation_Name: Workstation name
  • Database_Name: Database context of the session
  • Application_Description: Application accessing SQL Server
  • SPECID: System Process ID with Execution Context ID

If you ever need to remember what the input parameters / output columns are and what they mean, you can simply execute the following:

EXECUTE dbo.usp_who5 '?'

Best of all, if there is any blocking occurring on the server it will come right to the top of the result set and show you the details immediately (which SPID is blocked and by who, which SPIDs are blocking other processes, which are running in parallelism).

I typically map the procedure to keyboard combinations in SQL Server in order to run it on the fly with various input parameter combinations.

Any friendly feedback is always welcome. Enjoy!

Total article views: 9753 | Views in the last 30 days: 21
Related Articles

What SQL Statements Are Currently Executing?

This article describes a utility that allows you to identify what SQL statements are currently execu...


Execute the Current Statement–SQL Prompt

This is a great little feature in SQL Prompt that I wrote about at SQLServerCentral. It’s Current st...


Blocking Issue:Insert blocking select statements

Insert blocking select statements


Executing batches asychronously?

discuss support for asynchronous batch execution in sql server 2000



Blocking & Blocked by


Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones