Script to Kill all the connections for the database in sqlserver2005

Posted: June 4, 2010 in Scripts to use

Below script will help you to kill all the existing connection coming to the user databases.
Copy and paste the script in Query window and change the DBName to the one that you want to troubleshoot.

DECLARE @DatabaseName nvarchar(50)
SET @DatabaseName = N”

DECLARE @SQL varchar(max)
SET @SQL = ”

SELECT @SQL = @SQL + ‘Kill ‘ + Convert(varchar, SPId) + ‘;’
FROM master..sysprocesses
WHERE DBId = DB_ID(@DatabaseName) AND SPId @@SPId

EXEC(@SQL)

Leave a comment