Created
June 10, 2025 13:00
-
-
Save cemerson/6e22b3222a2ef773d37dbb9c8f2cb40f to your computer and use it in GitHub Desktop.
SQL: Fix for Database stuck in Single User Mode
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
----- Fix SQL Single User DB/Locked/Etc | |
USE master; | |
GO | |
SET DEADLOCK_PRIORITY HIGH; | |
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; | |
DECLARE @CMD nvarchar(max); | |
WHILE 1=1 | |
BEGIN | |
SET @CMD = NULL; | |
SELECT @CMD = ISNULL(@CMD + CHAR(10), '') + CONCAT(N'KILL ', session_id) | |
FROM sys.dm_exec_requests | |
WHERE database_id = DB_ID('MySQLDB') AND session_id <> @@SPID; | |
IF @CMD IS NULL BREAK; | |
PRINT @CMD; | |
EXEC(@CMD); | |
END | |
ALTER DATABASE MySQLDB SET MULTI_USER WITH ROLLBACK IMMEDIATE; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment