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 @kill=@kill+'kill '+convert(varchar(5),spid)+';'
    from master..sysprocesses 
where dbid=db_id('MY_DATABASE_NAME');
exec (@kill);
Share your love