Important alert: (current site time 7/15/2013 7:05:34 PM EDT)
 

VB icon

Drop connections

Email
Submitted on: 5/29/2002 4:38:22 AM
By: Zoltan Tamas, Toth 
Level: Intermediate
User Rating: By 8 Users
Compatibility: SQL Server 7.0, SQL Server 6.5 and earlier
Views: 15384
author picture
(About the author)
 
     PLEASE VOTE! This script drops all connection of a database. If you have installed SQL 2k client tools, you must copy this script to: ..\Program Files\MS SQL Server\80\Tools\Templates\Query Analyzer\Drop_Connection.tql! Then you can use this template anytime from Query analyzer! (Click to "Object Browser", drag and drop the script, press "Replace Template Parameters" button and specify a database name)
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
--**************************************
-- Name: Drop connections
-- Description:PLEASE VOTE! This script drops all connection of a database. If you have installed SQL 2k client tools, you must copy this script to:
..\Program Files\MS SQL Server\80\Tools\Templates\Query Analyzer\Drop_Connection.tql!
Then you can use this template anytime from Query analyzer! (Click to "Object Browser", drag and drop the script, press "Replace Template Parameters" button and specify a database name)
-- By: Zoltan Tamas, Toth
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=478&lngWId=5--for details.--**************************************

DECLARE
@p_SPID int,
@p_SQL nvarchar(2000)
 DECLARE #cur_Processes CURSOR FOR
SELECT
 p.SPID
FROM 
 master.dbo.sysprocesses AS p
 JOIN master.dbo.sysdatabases AS d ON( d.dbid = p.dbid )
WHERE
 d.Name = '<Database Name, , Name>'
 OPEN #cur_Processes
 FETCH NEXT FROM #cur_Processes INTO @p_SPID
 WHILE @@FETCH_STATUS = 0 
 begin
SET @p_SQL = 'KILL ' + CONVERT( nvarchar(30), @p_SPID )
PRINT @p_SQL
EXECUTE( @p_SQL )
FETCH NEXT FROM #cur_Processes INTO @p_SPID
 end
 CLOSE #cur_Processes
 DEALLOCATE #cur_Processes


Other 7 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

3/5/2003 7:09:28 AMArnold Louw

Your code is great - just two little problems that I have - what about system connectons to a database like the connection of "LAZY WRITER" and secondly what about connections of your on host name surley you would not want to kill yourself. But as I said great code thats why you got 5 stars from me
(If this comment was disrespectful, please report it.)

 
5/8/2003 2:04:31 AMTristan B. Astillero

i just tried it out on sql server 2000 and it works. apparently, the close method for the ado.net connection does not release the connection and any other processes w/ it in sql server. i've not encountered any problem w/ it so far. thanks.
(If this comment was disrespectful, please report it.)

 
10/13/2005 7:15:59 AMPiyanan

I want the sourcecode for term project. Thank you for sourcecode.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.