How to kill all connections to a specific SQL Server database

SQL-Server

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);