join the MSSQLTips community

Today's Site Sponsor


 

SQL diagnostic manager gives us analysis, monitoring, alerting and reporting that would satisfy even the pickiest DBA!
 



Speed up SQL script deployment

Obtain Exclusive Access to a SQL Server Database

Written By: Jeremy Kadlec -- 5/21/2007 -- read/post comments -- print -- Bookmark and Share

Rating: (not rated yet) Rate

Problem
In your earlier tip (Die Sucker - Killing SQL Server Process Ids (spids)) you outlined an option to kill spids (system process ids) to perform database restores or database maintenance when exclusive use of the database is needed.  Do any other options exist?  What are the advantages and disadvantages to these approaches?  When should one approach be used over another?

Solution
The typical approach to gaining exclusive use of a database is to use the ALTER DATABASE command.  This command has a few different parameters that can be helpful without writing any custom code to gain exclusive or restricted access to a database.  Check out the options and examples below in addition to some high level option analysis.

ALTER DATABASE Options

  • SET - Determines the type of user access to the database
    • SINGLE_USER - Single user connection to the database
    • RESTRICTED_USER - Any number of users with db_owner or dbcreator or logins with sysadmin rights can connect to the database
    • MULTI_USER - Any number of users with rights to the database can connect to the database
  • WITH ROLLBACK - Determines how the exclusive access to the database will take place
    • ROLLBACK AFTER integer [SECONDS] - Rollback the spids after a particular number of seconds
    • ROLLBACK IMMEDIATE - Rollback the spids immediately
    • NO_WAIT - If all of the spids do not commit or rollback immediately the request to put the database in an exclusive state will fail

ALTER DATABASE Example

Option 1 (Restricted Access) - Restrict the database to users with db_owner, dbcreater or sysadmin rights and rollback the spids immediately
ALTER DATABASE DatabaseName
SET RESTRICTED_USER
WITH ROLLBACK IMMEDIATE
 
Option 2 (Exclusive Access) - Restrict the database to a single user and rollback the spids immediately
ALTER DATABASE DatabaseName
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE
 
Option 3 (Multi User Access) - Unrestricted database access
ALTER DATABASE DatabaseName
SET MULTI_USER
 

ALTER DATABASE - Advantages and Disadvantages

  • Advantages
    • Easily incorporated into scripts for database restoration processes
    • A fair amount of flexibility based on the options listed above without having to write any custom code
  • Disadvantages
    • Could potentially have a long rollback process
      • The typical resolution would be to use the NO_WAIT option
    • Could potentially kill spids that need to finish for business reasons that are unexpectedly running slow

Kill spid Script - Advantages and Disadvantages

  • Advantages
    • Can modify the script to determine if a specific application is running and determine if any spids should be killed or just wait for a finite period of time with a WAITFOR command
    • Can modify the script to conditionally issue the ALTER DATABASE statement as opposed to the KILL commands to determine if the database should be put in an exclusive state
    • Can modify the script to meet conditional needs
  • Disadvantages
    • Could potentially have a long rollback process
      • Since you are using custom code and have knowledge of your environment you could conditionally kill spids as opposed to just killing all spids
    • Could potentially kill your own spid
      • The typical resolution would be to connect to a database that will not have spids killed as a portion of the process

Next Steps

Readers Who Read This Tip Also Read Free Live Webcast Comment or Ask Questions About This Tip


Wasting time running multiple scripts against multiple SQL Servers manually?

Try SQL Multi Script and execute all those scripts with just one mouse click.

Execute multiple scripts against multiple SQL Servers with a single click

"Just tried SQL Multi Script and very impressed with it. Talk about reducing work load!"

Neil Abrahams SQL Server DBA/Developer

SQL Multi Script Red Gate Software - ingeniously simple tools

Get Our Tips Newsletter

We keep 50,000+ SQL Server professionals informed.



Red Gate Software - SQL Compare

Quickly and accurately deploy database changes with Red Gate’s SQL Compare. 2/3 of people who use a SQL comparison tool use Red Gate’s SQL Compare – the industry standard database comparison and deployment tool. “We rely on SQL Compare for every deployment.” Paul Tebbut, Technical Lead, Universal Music Group

Download now!

More SQL Server Tools
SQL Backup

SQL safe backup

SQL comparison toolset

SQL Prompt

SQL diagnostic manager


Sponsor Information
Free SQL Server performance monitoring dashboard – Idera SQL check

Try Red Gate SQL Backup Pro for smaller, more robust SQL Server backups. Download a free trial now!

You don't know, what you don't know about SQL Server... Customized Consulting and Training

Do you love MSSQLTips and wish there was a SharePoint version?

Free whitepaper - How to Achieve 40:1 Backup Compression with LiteSpeed® for SQL Server’s



Copyright (c) 2006-2010 Edgewood Solutions, LLC All rights reserved
privacy statement | disclaimer | copyright | advertise | write for mssqltips | feedback | about
Some names and products listed are the registered trademarks of their respective owners.


CareerQandA.com | MSSharePointTips.com | MSSQLTips.com