Posted By

samwa on 09/22/10


Tagged

sql sql2005


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

samwa
geekatron
Tyster


Drop all connections to a sql 2005 database


 / Published in: SQL
 

URL: http://www.julian-kuiters.id.au/article.php/sql-server-2005-snippet-drop-db-conn

  1. USE master
  2. go
  3.  
  4. DECLARE @dbname sysname
  5.  
  6. SET @dbname = 'name of database you want to drop connections from'
  7.  
  8. DECLARE @spid int
  9. SELECT @spid = min(spid) FROM master.dbo.sysprocesses WHERE dbid = db_id(@dbname)
  10. WHILE @spid IS NOT NULL
  11. BEGIN
  12. EXECUTE ('KILL ' + @spid)
  13. SELECT @spid = min(spid) FROM master.dbo.sysprocesses WHERE dbid = db_id(@dbname) AND spid > @spid
  14. END

Report this snippet  

You need to login to post a comment.