Created
March 12, 2025 15:06
-
-
Save nanoDBA/850800388c32ddc6f10172098e491572 to your computer and use it in GitHub Desktop.
Dump sp_WhoIsActive to a table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- ████████ FILE: monitor_active_sessions.sql ████████ | |
-- 🎯 MISSION OBJECTIVE: Real-time monitoring of active SQL Server sessions. | |
-- - If the table **does not exist**, create it. | |
-- - If the table **exists**, append new data. | |
-- - If you need a **hard reset**, uncomment the DROP TABLE line. | |
-- | |
-- 🔧 USAGE: | |
-- 🎮 Execute in SSMS or a SQL Agent job for continuous ops. | |
-- 🔬 Uncomment DROP TABLE if you want to refresh the dataset. | |
-- 🏗️ SYSTEM INITIALIZATION: Declare key variables | |
DECLARE @schema NVARCHAR(MAX), | |
@createTableSQL NVARCHAR(MAX), | |
@destinationTable NVARCHAR(200) | |
SET @destinationTable = N'temp_monitoring_output' | |
-- 🛑 SAFETY SWITCH: Uncomment to **reset** the table before use | |
-- PRINT '⚠️ Nuking the table. BOOM!' | |
-- DROP TABLE IF EXISTS [dbo].[@destinationTable] | |
-- 🕵️ DETECT IF TABLE EXISTS | |
IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = @destinationTable) | |
BEGIN | |
-- 🚀 DEPLOY SCANNERS: Call sp_whoisactive to capture the current activity schema | |
EXEC sp_whoisactive | |
@get_outer_command = 1, | |
@find_block_leaders = 1, | |
@output_column_list = '[dd hh:mm:ss.mss][percent_complete][host_name][session_id][login_name][status][sql_text][wait_info][blocking_session_id][blocked_session_count][%]', | |
@sort_order = '[blocked_session_count] DESC, [start_time] ASC', | |
@return_schema = 1, | |
@schema = @schema OUTPUT | |
-- 🎯 LAUNCH TABLE CREATION: Replace placeholder with actual table name | |
SET @createTableSQL = REPLACE(@schema, '<table_name>', @destinationTable) | |
PRINT '🛠️ Creating table: ' + @destinationTable | |
EXEC sp_executesql @createTableSQL | |
END | |
ELSE | |
BEGIN | |
PRINT '🛠️ Table already exists. Skipping creation and logging new data.' | |
END | |
-- 🔄 DATA INGESTION: Insert active session data into the tracking table | |
EXEC sp_whoisactive | |
@get_outer_command = 1, | |
@find_block_leaders = 1, | |
@output_column_list = '[dd hh:mm:ss.mss][percent_complete][host_name][session_id][login_name][status][sql_text][wait_info][blocking_session_id][blocked_session_count][%]', | |
@sort_order = '[blocked_session_count] DESC, [start_time] ASC', | |
@destination_table = @destinationTable | |
-- 📡 DATA RETRIEVAL: Read the table contents for further analysis | |
PRINT '📊 Retrieving active session data:' | |
-- Repurpose @createTableSQL to use in SELECT stmt | |
SET @createTableSQL = N'SELECT * FROM ' + @destinationTable | |
EXEC sp_executesql @createTableSQL | |
-- 🛑 END OF LINE. 🛑 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment