Add new comment

SQL Server: kill all database connections

How to kill all database connections before dropping it?

USE master;
GO
DECLARE @sql nvarchar(max);
WHILE 1=1 
BEGIN
    SELECT TOP 1 @sql = N'KILL ' + convert(nvarchar(10), spid) 
    FROM sysprocesses WHERE dbid = DB_ID('my_database');
    IF @@rowcount = 0  BREAK;
    EXEC sp_executesql @sql;
END;
DROP DATABASE IF EXISTS my_database;