{"id":733,"date":"2004-06-07T00:02:11","date_gmt":"2004-06-07T00:02:11","guid":{"rendered":"\/blogs\/kimberly\/post\/Kicking-users-out-of-a-database-for.aspx"},"modified":"2004-06-07T00:02:11","modified_gmt":"2004-06-07T00:02:11","slug":"kicking-users-out-of-a-database-for","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/kicking-users-out-of-a-database-for\/","title":{"rendered":"Kicking users out of a database for&#8230;"},"content":{"rendered":"<p><P>All sorts of purposes: maintenance, rebuilding the database objects, dropping\/recreating the database in test environments, changing a state option like readonly (did you know that no users can be using the database when you make this change to RO)&#8230;. <\/P><br \/>\n<P>Anyway, lots of reasons and this has always been the case. In previous releases we used to create sp_ (<EM>you know this is a special naming convention for objects in master that can be referenced in any database WITHOUT three-part naming<\/EM>) stored procedures that would detect the users using a database and either KILL <EM>spid<\/EM> directly or do a net send and then use WAITFOR DELAY to give the users a couple of seconds\/minutes\/whatever, to complete. Well, procedures like that are still useful (because you can set the database to &#8220;dbo use only,&#8221; warn the user, and then give them some time&#8230; However, warning them typically uses xp_cmdshell (which a lot of you don&#8217;t allow) and sometimes you don&#8217;t want to wait and\/or give them much time :) :).<\/P><br \/>\n<P>So &#8211; did you know that ALTER DATABASE in SQL Server 2000 has been changed to allow termination options for STATE changes. The termination options allow one of three types of termination:<\/P><br \/>\n<UL><br \/>\n<LI>Give the users <EM>n<\/EM> number of <STRONG>seconds<\/STRONG> to wrap things up<br \/>\n<UL><br \/>\n<LI><FONT face=\"Courier New\">ALTER DATBASE <EM>dbname<\/EM> SET READ_ONLY WITH ROLLBACK AFTER <\/FONT><\/LI><\/UL><br \/>\n<LI>Kick them out immediately<br \/>\n<UL><br \/>\n<LI><FONT face=\"Courier New\">ALTER DATBASE <EM>dbname<\/EM> SET READ_ONLY WITH ROLLBACK IMMEDIATE<\/FONT><FONT face=\"Courier New\"><\/FONT><\/LI><\/UL><br \/>\n<LI>Or, optionally you can have your process NOT WAIT<br \/>\n<UL><br \/>\n<LI><FONT face=\"Courier New\">ALTER DATBASE <EM>dbname<\/EM> SET READ_ONLY WITH NO_WAIT<\/FONT><\/LI><\/UL><\/LI><\/UL><br \/>\n<P>If no options are set and the process cannot get exclusive use of the database then it will till it can &#8211; indefinitely. Some database option changes generate a &#8220;could not get exclusive use&#8221; error&nbsp;but READ_ONLY (and a few others) don&#8217;t. If this is in an automated script\/process this can cause you a lot of grief. (<EM>In fact, I&#8217;m adding this note about 90 minutes after I created this blog entry&#8230;because my QA window is still trying to get exclusive access to pubs &#8211; which I was setting to readonly just to see if it would eventually time out&#8230;.well, 90 minutes and still running it hasn&#8217;t. I think I&#8217;m going to kill it!<\/EM>)<\/P><br \/>\n<P>For a good example of how to change certain state changes when creating batch processes see, <a href=\"http:\/\/staff.newtelligence.net\/playground\/clemensv\/kim\/content\/binary\/DB%20Alter%20for%20Batch%20Operation.sql\" class=\"broken_link\">DB Alter for Batch Operation.sql (6.3 KB)<\/a>.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>All sorts of purposes: maintenance, rebuilding the database objects, dropping\/recreating the database in test environments, changing a state option like readonly (did you know that no users can be using the database when you make this change to RO)&#8230;. Anyway, lots of reasons and this has always been the case. In previous releases we used [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[78],"tags":[],"class_list":["post-733","post","type-post","status-publish","format-standard","hentry","category-tips"],"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/posts\/733","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/comments?post=733"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/posts\/733\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/media?parent=733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/categories?post=733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/tags?post=733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}