Posted By

sommertim on 09/08/11


Tagged

sql tsql MS SQL-Server


Versions (?)

List and kill active DB connections


 / Published in: SQL
 

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

  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  

You need to login to post a comment.