Created
May 3, 2022 20:37
-
-
Save MarkPryceMaherMSFT/562a37cdcf256ed82b003c331d83303c to your computer and use it in GitHub Desktop.
Proc to kill long running transactions
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
CREATE PROC [dbo].[sp_kill_transactions] AS | |
IF OBJECT_ID('tempdb..#stats_ddl') IS NOT NULL | |
BEGIN; | |
DROP TABLE #stats_ddl; | |
END; | |
CREATE TABLE #stats_ddl | |
WITH | |
( | |
DISTRIBUTION = ROUND_ROBIN | |
) | |
AS | |
SELECT distinct 'kill ''' + waits.session_id + '''' as sessionid, ROW_NUMBER() | |
OVER(ORDER BY (SELECT NULL)) AS [seq_nmbr] | |
FROM sys.dm_pdw_waits waits | |
JOIN sys.dm_pdw_exec_requests requests | |
ON waits.request_id=requests.request_id | |
WHERE waits.[type] = 'ExclusiveUpdate' | |
and datediff(s,requests.start_time,getdate()) > 600 -- time in seconds | |
DECLARE | |
@i INT = 1 | |
, @t INT = (SELECT COUNT(*) FROM #stats_ddl) | |
, @sessionid NVARCHAR(50) = N''; | |
WHILE @i <= @t | |
BEGIN | |
SET @sessionid = (SELECT sessionid FROM #stats_ddl WHERE seq_nmbr = @i); | |
PRINT @sessionid | |
BEGIN TRY | |
EXEC sp_executesql @sessionid | |
END TRY | |
BEGIN CATCH | |
PRINT 'Opps - something went wrong....' | |
END CATCH | |
SET @i+=1; | |
END | |
DROP TABLE #stats_ddl; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment