Important alert: (current site time 7/15/2013 8:04:47 PM EDT)
 

VB icon

xOustUsers

Email
Submitted on: 12/4/2002 5:45:59 PM
By: Bruce Donohue 
Level: Advanced
User Rating: By 2 Users
Compatibility: SQL Server 2000, SQL Server 7.0
Views: 11073
 
     OustUser is a Stored Procedure that kills all of the processes for a specified database. I mainly find this useful when I have to restore a database; someone always leaves some connection open and causes the restore to fail.
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
--**************************************
-- for :xOustUsers
--**************************************
xOustUsers
Bruce Donohue, November 2002
weasel@batzx.com
Freeware. Enjoy.
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: xOustUsers
-- Description:OustUser is a Stored Procedure that kills all of the processes for a specified database. I mainly find this useful when I have to restore a database; someone always leaves some connection open and causes the restore to fail.
-- By: Bruce Donohue
--
-- Inputs:The database name to oust users from
--
-- Returns:Returns a single record with the value 'Ousted' if successful.
Raises custom error messages for errors encountered.
--
-- Assumes:You must be SA to create and run this.
--
-- Side Effects:Cannot oust itself. If you want to oust everyone in Northwind, you cannot run the SP from the Northwind database.
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=606&lngWId=5--for details.--**************************************

use master
go
if exists (select id from sysobjects where id = object_id('dbo.xOustUsers') )
	drop procedure dbo.xOustUsers
go
create procedure dbo.xOustUsers
	@sDB	varchar(30)
as
set nocount on
begin
declare @sVal	varchar(60),
	@nCount	int
	
if exists(select dbid from master..sysdatabases where name = @sDB )
	begin
	if @sDB <> db_name()
		begin
	
		Declare cKill Cursor for 
			select 'kill ' + convert(varchar,spid)
			from 	master..sysprocesses sp,
				master..sysdatabases sd
			where	sp.dbid = sd.dbid and
				sd.name = @sDB
		Open cKill
		 
		fetch next from cKill into @sVal
		 
		While @@FETCH_STATUS = 0
			Begin
			
			exec(@sVal)
			
			fetch next from cKill into @sVal
			End
		 
		Close cKill
		Deallocate cKill
		select @nCount = count(sd.dbid)
		from 	master..sysprocesses sp,
			master..sysdatabases sd
		where	sp.dbid = sd.dbid and
			sd.name = @sDB
		if @nCount = 0 
			begin
			select 'Ousted' Successful
			end
		else
			begin
			select 'Cannot Kill SPID:' + convert(varchar,sp.spid) + ' ' + program_name Error
			from 	master..sysprocesses sp,
				master..sysdatabases sd
			where	sp.dbid = sd.dbid and
				sd.name = @sDB
			end
		end
	else
		begin
		raiserror 50001 'You cannot run this in the database you wish to oust everyone from'
		end
	end
else
	begin
	raiserror 50002 'Invalid Database Name'
	end
end
GO
 
--exec master..xOustUsers 'NorthWind'


Other 6 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 Advanced 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

12/5/2002 9:10:12 AMSpammer_Spammer

Cool Bruce!

Thanks for you valuable submissions...

Spammer_Spammer
(If this comment was disrespectful, please report it.)

 
12/6/2002 10:57:06 AM

This code is quite nice .. much better than the my script to do process killing.
(If this comment was disrespectful, please report it.)

 
2/18/2003 6:37:25 AMBOKIJANA

MMMMMMMMMMMMMMMMMMMMMMMMMM
(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.