Drop all SQL Database Connections

I must have run this a dozen times in the last few days. Dead useful, which is why I’m posting it. Mostly for my own reference

USE master
GO

DECLARE @kill varchar(8000) = '';
SELECT @kill = @kill + 'kill ' + CONVERT(varchar(5), spid) + ';'
FROM master..sysprocesses 
WHERE dbid = db_id('MyDatabaseName')

EXEC(@kill);

Leave a Reply