xxxxxxxxxx
DECLARE @sql NVARCHAR(MAX) = N'';
-- Generate KILL statements for each active connection
SELECT @sql += N'KILL ' + CAST(spid AS NVARCHAR) + N';'
FROM sys.sysprocesses
WHERE dbid = DB_ID('YourDatabaseName') -- Replace 'YourDatabaseName' with the name of your database
AND spid > 50; -- Exclude system processes
-- Execute the generated KILL statements
EXEC sp_executesql @sql;
xxxxxxxxxx
USE [master];
DECLARE @kill varchar(8000) = '';
SELECT @kill = @kill + 'kill ' + CONVERT(varchar(5), session_id) + ';'
FROM sys.dm_exec_sessions
WHERE database_id = db_id('MyDB')
EXEC(@kill);