Thursday, April 24, 2014

Exclusive access could not be obtained because the database is in use SQl 2005


If you get the error:
Exclusive access could not be obtained because the database is in use:



It is because when you restore the a database you should have Exclusive lock, you can kill the process which are using database. otherwise use the command showed below.
  
--
-- This should give you Exculusive lock
--
  
USE MASTER
ALTER DATABASE  MyDatabase   SET SINGLE_USER WITH ROLLBACK IMMEDIATE;

-- Do your work

-- Reenable the database
ALTER DATABASE MyDatabase SET MULTI_USER WITH ROLLBACK IMMEDIATE;

--
--