List and kill active DB connections


/ Published in: SQL
Save to your folder(s)

Kill connections to a database to perform maintenance that requires a connectionless DB


Copy this code and paste it in your HTML
  1. 1) GET the processes attached TO that DATABASE:
  2.  
  3. SELECT spid FROM master..sysprocesses WHERE dbid = DB_ID(@DatabaseName)
  4. AND spid != @@SPID
  5.  
  6. Note : @@SPID IS your connected instance Service Process id "SPID"
  7.  
  8. 2) Perform KILL operation IN a loop OR USE cursor
  9.  
  10. EXEC('KILL '+RTRIM(@spid) )

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.