Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
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)

usp_who5

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 (includes sleeping SPIDs with open transactions)
  • B - Blocked sessions only
  • S - Exclude sleeping SPIDs with open transactions
  • X - Exclude system processes
  • @Database_Name: Limit the result set to a specific database (use ---------- for NULL database names)
  • @Exclude_Lock: Suppress locking details from the output (can increases procedure performance on busy servers; defaulted to 1)
  • @Exclude_Plan: Suppress execution plan details from the output (can increases procedure performance on busy servers; defaulted to 1)
  • @Exclude_SQL: Suppress SQL statement details from the output (can increases procedure performance on busy servers; defaulted to 0)
  • @Exclude_SQL_XML: Suppress SQL statement XML details from the output (can increases procedure performance on busy servers; defaulted to 1)
  • @Login: Limit the result set to a specific Windows user name (if populated, otherwise by SQL Server login name)
  • @SPID: Limit the result set to a specific session
  • @SQL_Text: Limit the result set to SQL statements containing specific text

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
  • Database_Name: Database context of the session
  • Running: Indicates if the session is executing (X), waiting ([]), inactive (blank), inactive with open transactions (•), a background task (--), or not defined (N/A)
  • Blocking: Blocking indicator (includes type of block and SPID list)
  • Status: Status of the session -> request -> task
  • Object_Name: Object being referenced (blank for ad hoc and prepared statements)
  • Command: Command executed
  • SQL_Statement_Batch: Batch statement of the session
  • SQL_Statement_Current: Current statement of the session
  • Open_Trans: Open transactions for the process
  • Isolation_Level: Isolation level of the session
  • Wait_Time: Current wait time (DAYS HH:MM:SS)
  • Wait_Type: Current wait type
  • Last_Wait_Type: Previous wait type
  • SQL_Statement_Batch_XML: Same as "SQL_Statement_Batch" but in XML format
  • SQL_Statement_Current_XML: Same as "SQL_Statement_Current" but in XML format
  • SQL_Handle: Identifier for the executing batch or object
  • 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_Total: Logical reads performed since login
  • Logical_Reads_Current: Logical reads performed by the current process
  • Physical_Reads_Total: Physical reads performed since login
  • Physical_Reads_Current: Physical reads performed by the current process
  • Writes_Total: Writes performed since login
  • Writes_Current: Writes performed by the current process
  • Nesting_Level: Nesting level of the statement currently executing
  • Last_Row_Count: Row count produced by the last statement executed
  • Allocated_Memory_MB: Memory allocated to the query in megabytes
  • Pages_Used: Pages in the procedure cache allocated to the process
  • Since_SPID_Login: Elapsed time since the client logged in (DAYS HH:MM:SS)
  • Since_Last_Batch_Start: Elapsed time since the last request began (DAYS HH:MM:SS)
  • Since_Last_Batch_End: Elapsed time since the last completion of a request (DAYS HH:MM:SS)
  • Query_Plan: Execution plan of the session (in XML format)
  • Plan_Cache_Object_Type: Type of object in the cache
  • Plan_Object_Type: Plan object type
  • Plan_Times_Used: Times the plan has been utilized since its creation
  • Plan_Size_MB: Size consumed by the plan in megabytes
  • Plan_Handle: Identifier for the in-memory plan
  • 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 (applies to a limited set of commands)
  • Command_Completion: Estimated completion time for the command
  • Command_Time_Left: Time left before the command completes (DAYS HH:MM:SS)
  • 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
  • Host_Name: Name of the client workstation specific to a session
  • 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"
  • Application_Description: Application accessing SQL Server
  • Previous_Error: Previous error returned by the session
  • System_Process: Indicates if the session is a system process
  • 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 N'?'

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.

Any friendly feedback is always welcome. Enjoy!

Total article views: 11736 | Views in the last 30 days: 168
 
Related Articles
ARTICLE

What SQL Statements Are Currently Executing?

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

BLOG

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...

FORUM

Executing batches asychronously?

discuss support for asynchronous batch execution in sql server 2000

ARTICLE

The power of batching Transactions

This article analyzes the performance gains of batching multiple DML statements into a single transa...

ARTICLE

Execute SQL job through batch file

This article will show you how you can execute a SQL job with one click, allowing anyone to do this....

Tags
activity    
blocking    
enhancement    
monitoring    
processes    
sp_who    
sp_who2    
 
Contribute