Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Killing other processes connected to an MSSQL database.
declare @db_name varchar(100) = 'dbname';
declare @kill_commands varchar(max) = '';
@kill_commands=@kill_commands+'kill '+convert(varchar(5),spid)+';'
from master..sysprocesses
spid <> @@SPID -- avoid to kill the current process
and dbid=db_id(@db_name)
print 'Executing: ' + @kill_commands;
exec (@kill_commands);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment