SQL: Drop / Kill Database Connections

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

If you need to drop database connections for a specific database, go read on..
This has been tested for Microsoft SQL Server 2005 and Microsoft SQL Server 2008.
Logo_Ms_SQL

Code starts here

use master
declare @vcdbname varchar(50)
/* @vcdbname => set it to the database you want the connections to be dropped.. */
Set @vcdbname = ‘cxnIM’

set nocount on
declare Users cursor for
select spid
from master..sysprocesses
where db_name(dbid) = @vcdbname
declare @spid int, @str varchar(255)
open users
fetch next from users into @spid
while @@fetch_status <> -1
begin
   if @@fetch_status = 0
   begin
   print @spid
      set @str = ‘kill ‘ + convert(varchar, @spid)
      exec (@str)
   end
   fetch next from users into @spid
end
deallocate users

and ends here

1 thought on “SQL: Drop / Kill Database Connections

  1. Pingback: ..::\ www.christiano.ch //::.. » SQL: List Database Connections for specific user

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.