T-SQL: Kill all connections to a specific database

Bschelst/ February 11, 2019/ SQL-Server/ 0 comments

Sometimes you need to do maintenance on a database, and you don’t really want to use single user mode or disable accounts. In that situation you could kill all connections to a specific database.
In order to achieve that, you can use the t-sql code below.

declare @kill varchar(8000) = '';
select @[email protected]+'kill '+convert(varchar(5),spid)+';'
    from master..sysprocesses 
where dbid=db_id('MY_DATABASE_NAME');
exec (@kill);
Share this Post

Leave a Comment

Your email address will not be published. Required fields are marked *