SQL: Generate Kill Command

This handy SQL query list all connections to an mssql database and a corresponding kill command. The kill command will just be printed, not executed.

DECLARE @database_name NVARCHAR(255);
SET @database_name = 'name of database'
SELECT
    DB_NAME(dbid) as [DBName],
    [Hostname], 
    [program_name], 
    [loginame], 
    [spid], 
    [login_time], 
    [last_batch]
  ,'kill ' + STR(spid) as CMD
FROM
    sys.sysprocesses
WHERE
    dbid = DB_ID(@database_name)
ORDER BY [Hostname];