Log and Filter sp_who2 Results in SQL Server

When managing SQL Server performance, the built-in sp_who2 procedure is a commonly used tool for quickly checking active processes and their resource usage. However, it doesn’t always give you much flexibility when analyzing SQL sessions.

In this post, I’m sharing a simple SQL script that logs sp_who2 results into a temporary table, allowing you to filter and sort sessions in SQL for review. This can be especially useful when investigating blocking issues, long-running queries, or hidden inactive SPIDs that standard scripts might miss. Unlike custom queries that only capture currently active sessions, this method ensures you see a more complete picture of SQL activity, including sessions that may still be holding locks or consuming resources in the background.


Logging sp_who2 Results into a Temp Table

The following script logs the output of sp_who2 into a temporary table, allowing for easier querying, filtering, and sorting of session information:

Log SP_Who2 to Table SQL Server

Once executed, this script captures all active sessions at that moment, letting you filter by database, order by SPID, or focus on blocked processes with simple queries.

Logging the output of sp_who2 into a table ensures you catch everything, even non-active SPIDs that could be affecting system performance without showing up in real-time monitoring.

This SQL script has helped me in various occasions when diving deep into troubleshooting issues. I hope it also serves you well too!


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts
Categories
Tags

Always On Availability Groups (AAG) (4) AWS (4) AWS Redshift (6) Certificates & Encryption (3) Change Data Capture (CDC) (2) Database Admin (72) Database Backups & Recovery (14) Database Mirroring (2) Deleting Data (1) Error Messages (5) Git Commands (6) Importing & Exporting Data (2) Linked Servers (3) Linux Admin (2) Logging & Monitoring (1) Measuring Databases (10) Microsoft Patching (2) MySQL (4) Postgres (6) PowerShell Scripts (1) SQL Server Agent (5) SQL Server Database Files (1) SQL Server Data Types (2) SQL Server Management Studio (SSMS) (15) SQL Server Network Connectivity (3) SQL Server on Linux (1) SQL Server Patching (2) SQL Server Performance (6) SQL Server Permissions (2) SQL Server Processes (SPIDs) (7) SQL Server Replication (2) SQL Server Scripts (13) Windows Admin (21) Windows Authentication (2) Windows Automation (1) Windows Events (2) Windows Firewall (4) Windows Subsystem for Linux (WSL) (18)