Killing other processes connected to an MSSQL database

declare @db_name varchar(100) = 'dbname';
declare @kill_commands varchar(max) = '';

select
@kill_commands=@kill_commands+'kill '+convert(varchar(5),spid)+';'
from master..sysprocesses
where
spid <> @@SPID -- avoid to kill the current process
and dbid=db_id(@db_name)

print 'Executing: ' + @kill_commands;
exec (@kill_commands);
https://gist.github.com/szunyog/7941990

No comments: