0

I am currently connected to a customer database via the front end using there application and I run the following query which displays active connections:

SELECT DB_NAME(DBID) AS DBNAME,
       COUNT(DBID)   AS NUMBEROFCONNECTIONS,
       LOGINAME      AS LOGINNAME
FROM   SYS.SYSPROCESSES
WHERE  DBID > 0
GROUP  BY DBID, LOGINAME 

I have the ability to run SQL in the front end of the application therefore how can i kill all active connections based on a DB_NAME(DBID) = 'CustomerDB' using SQL?

PriceCheaperton
  • 5,071
  • 17
  • 52
  • 94

2 Answers2

0

KILL command may not work for the newly established connection. You can try taking the database to Single_User mode rather using an ALTER statement like

ALTER DATABASE CustomerDB
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
Rahul
  • 76,197
  • 13
  • 71
  • 125
-1
USE master
GO

DECLARE @kill varchar(8000) = '';
SELECT @kill = @kill + 'kill ' + CONVERT(varchar(5), spid) + ';'
FROM master..sysprocesses 
WHERE dbid = db_id('MyDB')

EXEC(@kill);

Credits: Script to kill all connections to a database (More than RESTRICTED_USER ROLLBACK)

Community
  • 1
  • 1
japzdivino
  • 1,736
  • 3
  • 17
  • 25