Thursday, October 15, 2009

droping all connections with sql server

you can kill all the processes using a database:

USE master
go

DECLARE @dbname sysname

SET @dbname = 'name of database you want to drop connections from'

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




If you want to drop all the connections to a database immediately

USE master
GO

ALTER DATABASE database name
SET OFFLINE WITH ROLLBACK IMMEDIATE
ALTER DATABASE database name
SET ONLINE




or if you are in a hurry there is an option that says"drop all database connections" as a checkbox in the window displayed for the detach task and you just check it and does close the connections.

No comments: