﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Discuss Content Posted by Brian Knight / Article Discussions / Article Discussions by Author  / Kill All Users in A Given Database / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 25 May 2013 18:52:11 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Kill All Users in A Given Database</title><link>http://www.sqlservercentral.com/Forums/Topic401827-31-1.aspx</link><description>Michael,Very slick! Thank you!Jeff</description><pubDate>Tue, 21 Sep 2010 11:56:09 GMT</pubDate><dc:creator>JRoughgarden</dc:creator></item><item><title>RE: Kill All Users in A Given Database</title><link>http://www.sqlservercentral.com/Forums/Topic401827-31-1.aspx</link><description>Very handy sp, i modified it so it validates the database name parameter against a whitelist of DBs we want to allow this to run against.Always use whitelists. Blacklists eventually result in something happening accidentally because you forgot to maintain the list.</description><pubDate>Mon, 27 Apr 2009 18:14:58 GMT</pubDate><dc:creator>stevekgoodwin</dc:creator></item><item><title>RE: Kill All Users in A Given Database</title><link>http://www.sqlservercentral.com/Forums/Topic401827-31-1.aspx</link><description>Thanks alot, I need this just now and it worked perfectly.</description><pubDate>Wed, 24 Sep 2008 01:14:28 GMT</pubDate><dc:creator>Lars Søe Mikkelsen</dc:creator></item><item><title>RE: Kill All Users in A Given Database</title><link>http://www.sqlservercentral.com/Forums/Topic401827-31-1.aspx</link><description>This is a much simpler way to get everyone out of the database:[code]-- Get rid of all DB users use masteralter database MyDatabase set offline with rollback immediate-- Bring DB back online in single user mode, and connect to DBalter database MyDatabase set online, single_user with rollback immediateuse MyDatabase-- do stuff ---- Return DB to multi user modeuse masteralter database MyDatabase set multi_user with rollback immediate[/code]</description><pubDate>Fri, 04 Apr 2008 14:56:22 GMT</pubDate><dc:creator>Michael Valentine Jones</dc:creator></item><item><title>RE: Kill All Users in A Given Database</title><link>http://www.sqlservercentral.com/Forums/Topic401827-31-1.aspx</link><description>Nice script!RequestID added to #tmpUsers for SQL2005-----------------------Alter PROCEDURE KillALLUsers @dbname varchar(50) asSET NOCOUNT ONDECLARE @strSQL varchar(255)PRINT 'Killing Users'PRINT '-----------------'CREATE table #tmpUsers(spid int,eid int,status varchar(30),loginname varchar(50),hostname varchar(50),blk int,dbname varchar(50),cmd varchar(30),request_id int)INSERT INTO #tmpUsers EXEC SP_WHODECLARE LoginCursor CURSORREAD_ONLYFOR SELECT spid, dbname FROM #tmpUsers WHERE dbname = @dbnameDECLARE @spid varchar(10)DECLARE @dbname2 varchar(40)OPEN LoginCursorFETCH NEXT FROM LoginCursor INTO @spid, @dbname2WHILE (@@fetch_status &amp;lt;&amp;gt; -1)BEGIN	IF (@@fetch_status &amp;lt;&amp;gt; -2)	BEGIN	PRINT 'Killing ' + @spid	SET @strSQL = 'KILL ' + @spid	EXEC (@strSQL)	END	FETCH NEXT FROM LoginCursor INTO  @spid, @dbname2ENDCLOSE LoginCursorDEALLOCATE LoginCursorDROP table #tmpUsers</description><pubDate>Fri, 04 Apr 2008 14:32:57 GMT</pubDate><dc:creator>Sam Hewawasam </dc:creator></item><item><title>Kill All Users in A Given Database</title><link>http://www.sqlservercentral.com/Forums/Topic401827-31-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/scripts/Lock+and+Connection+Management/30030/"&gt;Kill All Users in A Given Database&lt;/A&gt;[/B]</description><pubDate>Sun, 23 Sep 2007 21:35:25 GMT</pubDate><dc:creator>Brian Knight</dc:creator></item></channel></rss>