Friday, November 25, 2011

Kill all connections to a Microsoft SQL Server DB


-- Create the sql to kill the active database connections
declare @execSql varchar(1000), @databaseName varchar(100)
-- Set the database name for which to kill the connections
set @databaseName = 'Massive.dev'

set @execSql = ''
select  @execSql = @execSql + 'kill ' + convert(char(10), spid) + ' '
from    master.dbo.sysprocesses
where   db_name(dbid) = @databaseName
     and
     DBID <> 0
     and
     spid <> @@spid
exec(@execSql)

Source:
http://blog.tech-cats.com/2008/01/kill-all-database-connections-to-sql.html