Saturday, December 12, 2015

How to Kill all Active Connections to the SQL Server Database

How to Kill all Active Connections to the SQL Server Database

use master
DECLARE @dbname sysname
SET @dbname = 'Your DB Name'

DECLARE @spid int
SELECT @spid = min(spid) from master.dbo.sysprocesses where dbid = db_id(@dbname)
WHILE @spid IS NOT NULL
BEGIN
EXECUTE ('KILL ' + @spid)
SELECT @spid = min(spid) from master.dbo.sysprocesses where dbid = db_id(@dbname) AND spid > @spid
END


No comments:

Post a Comment