Print

Restore MSSQL Error: Database is in use

What?
This is an article on how to do a restore from backup on a database but when you get the error: "... database is in use".
copyraw
-- SQL Server 2005
EXEC SP_WHO          // details on who is logged in
GO

-- SQL Server 2008
EXEC SP_WHO2         // even more details
GO

-- Run as database owner to see ALL connected processes as well.
  1.  -- SQL Server 2005 
  2.  EXEC SP_WHO          // details on who is logged in 
  3.  GO 
  4.   
  5.  -- SQL Server 2008 
  6.  EXEC SP_WHO2         // even more details 
  7.  GO 
  8.   
  9.  -- Run as database owner to see ALL connected processes as well. 


Why?
For every DBA this is a doddle and doesn't warrant its own article but for those of us who merely use SQL Server Management Studio (SSMS) and Microsoft's SQL Server 2008 R2 for development purposes, the once-in-a-blue-moon restore from backup process is quickly forgotten.

How?
You need to set the database to single-user mode.

  1. In SSMS, connect to an instance of the SQL Server Database Engine, and then expand that instance.
  2. Right-click the database to change, and then click Properties.
  3. In the Database Properties dialog box, click the Options page.
  4. From the Restrict Access option, select Single.
  5. If other users are connected to the database, an Open Connections message will appear. To change the property and close all other connections, click Yes.
Restoring
  1. in SSMS, Right-click on the database
  2. Select Tasks > Restore... > Database
  3. Specify the "to a point in time" if you don't want to restore from the most recent backup and double-check the "Start/Finish Date" is the one you want.
  4. OK

Searches

Sources

Category: Databases :: Article: 491