Skip to content

Instantly share code, notes, and snippets.

@petesql
Created February 25, 2025 18:32
Show Gist options
  • Save petesql/f159eb820f9cca943b1e91ff0a66c711 to your computer and use it in GitHub Desktop.
Save petesql/f159eb820f9cca943b1e91ff0a66c711 to your computer and use it in GitHub Desktop.
Logging sp_who2 Results to Table
-- logging sp_who2 results to table
CREATE TABLE #sp_who2 (
SPID INT,
Status VARCHAR(MAX),
Login VARCHAR(MAX),
HostName VARCHAR(MAX),
BlkBy VARCHAR(MAX),
DBName VARCHAR(MAX),
Command VARCHAR(MAX),
CPUTime INT,
DiskIO INT,
LastBatch VARCHAR(MAX),
ProgramName VARCHAR(MAX),
SPID1 INT,
REQUESTID INT
);
INSERT INTO #sp_who2
EXEC sp_who2;
-- Query results (filter optional)
SELECT *
FROM #sp_who2
-- WHERE DBName <> 'master' -- Exclude 'master' DB
ORDER BY SPID ASC;
-- Drop temp table
DROP TABLE #sp_who2;
@petesql
Copy link
Author

petesql commented Feb 25, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment