Skip to content

Instantly share code, notes, and snippets.

@EitanBlumin
Last active November 3, 2025 06:29
Show Gist options
  • Select an option

  • Save EitanBlumin/714da61f084d6366f34cdada6d2cdbf2 to your computer and use it in GitHub Desktop.

Select an option

Save EitanBlumin/714da61f084d6366f34cdada6d2cdbf2 to your computer and use it in GitHub Desktop.
Brute force database out of SINGLE_USER mode back into MULTI_USER mode
-- run this in query window 1:
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
SET DEADLOCK_PRIORITY HIGH;
GO
DECLARE @CMD nvarchar(max) = ''
SELECT @CMD = CONCAT(@CMD, CHAR(10), 'KILL ', session_id)
FROM sys.dm_exec_requests
WHERE database_id = DB_ID('MyDB') -- replace MyDB with your DB name
PRINT @CMD
EXEC (@CMD);
GO
-- replace MyDB with your DB name:
USE MyDB
ALTER DATABASE MyDB SET MULTI_USER;
-- run this in query window 2:
DECLARE @CMD nvarchar(max);
WHILE 1=1
BEGIN
SET @CMD = NULL;
SELECT @CMD = ISNULL(@CMD + CHAR(10), '')
+ CONCAT(N'KILL ', blocking_session_id)
FROM sys.dm_exec_requests
WHERE session_id = 1234 -- replace with the session id of query window 1
IF @CMD IS NULL BREAK;
RAISERROR(N'%s',0,1,@CMD) WITH NOWAIT;
IF @CMD <> 'KILL 0' EXEC(@CMD);
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment