Last active
June 26, 2017 18:22
-
-
Save LitKnd/9840a9b23adb53864f58a6a5324bdcc6 to your computer and use it in GitHub Desktop.
This file contains 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
DECLARE @whoisactive_table VARCHAR(4000) ; | |
DECLARE @schema VARCHAR(4000) ; | |
DECLARE @dsql NVARCHAR(4000) ; | |
SET @whoisactive_table = QUOTENAME ('##WhoIsActive_' + CAST(NEWID() as varchar(255))); | |
EXEC sp_WhoIsActive | |
@get_transaction_info = 1, | |
@output_column_list = '[block%][%]', | |
@get_plans = 1, | |
@find_block_leaders = 1, | |
@return_schema = 1, | |
@format_output = 0, | |
@schema = @schema OUTPUT ; | |
SET @schema = REPLACE(@schema, '<table_name>', @whoisactive_table) ; | |
PRINT @schema | |
EXEC(@schema) ; | |
EXEC sp_WhoIsActive | |
@get_transaction_info = 1, | |
@output_column_list = '[block%][%]', | |
@get_plans = 1, | |
@find_block_leaders = 1, | |
@format_output = 0, | |
@destination_table=@whoisactive_table; | |
SET @dsql = N' | |
IF ( | |
SELECT COUNT(*) | |
FROM ' + @whoisactive_table + N' | |
WHERE blocking_session_id IS NOT NULL | |
and datediff(mi,start_time, collection_time) > 15 | |
) > 0 | |
SELECT datediff(mi,start_time, collection_time) as duration_minutes, * | |
FROM ' + @whoisactive_table + N' OPTION (RECOMPILE);' | |
EXEC sp_executesql @dsql; | |
SET @dsql = N'DROP TABLE ' + @whoisactive_table + N';' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment