SQL Clone
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 - Blocking / 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 lock details from the output (can increases procedure performance on busy servers; defaulted to 1)
  • @Exclude_Log: Suppress log 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)
  • @Exclude_TXN: Suppress transaction details from the output (also suppresses log details due to an interdependency; 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

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

  • SPID: System Process 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, SPID list, and deadlock detection when applicable)
  • Status: Status of the session -> request
  • Object_Name: Object being referenced (blank for ad hoc and prepared statements)
  • Command: Command executed
  • Threads: Process thread count
  • SQL_Statement_Batch: Batch statement of the session
  • SQL_Statement_Current: Current statement of the session
  • 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
  • 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
  • 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
  • Transactions: Open transactions for the process
  • Transaction_ID: Transaction ID
  • Transaction_Time: Elapsed time since the transaction began (DAYS HH:MM:SS)
  • Transaction_Type: Type of transaction
  • Transaction_State: State of the transaction
  • Nesting_Level: Nesting level of the statement executing
  • TempDB_Session_Total_MB: Temp DB space used since login for the session in megabytes
  • TempDB_Session_Current_MB: Temp DB space currently used by the session in megabytes
  • TempDB_Task_Total_MB: Temp DB space used by the entire task in megabytes
  • TempDB_Task_Current_MB: Temp DB space currently used by the task in megabytes
  • Log_Database_Count: Databases involved in the transaction
  • Log_Records_All: Log records generated for the transaction (all databases)
  • Log_Reserved_MB_All: Log space reserved for the transaction in megabytes (all databases)
  • Log_Used_MB_All: Log space used for the transaction in megabytes (all databases)
  • Log_Details: Log usage details for the transaction per database (in XML format)
  • Lock_Timeout_Seconds: Lock timeout of the session
  • Lock_Details: Lock details of the session (in XML format)
  • Deadlock_Priority: Deadlock priority of the session
  • 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
  • Query_Plan: Execution plan of the session (in XML format)
  • Plan_Handle: Identifier for the in-memory plan
  • 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)
  • 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)
  • 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
  • System_Process: Indicates if the session is a system process
  • SPID: System Process 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 and / or deadlocking 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: 12664 | Views in the last 30 days: 8
 
Related Articles
FORUM

current sessions in database

current sessions in database

SCRIPT

Handling with currently running and blocking sessions

Checks currently running sessions and blocking queries with session information

ARTICLE

What SQL Statements Are Currently Executing?

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

SCRIPT

Current Users, Session Counts & Authentication Type

The script gives you , the number of current users and their session counts and authentication metho...

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

Tags
activity    
blocking    
deadlocking    
enhancement    
monitoring    
processes    
sp_who    
sp_who2    
 
Contribute