T-SQL: Kill all connections to a specific database

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);
Default image
Bart
Articles: 80