Exclusive access could not be obtained because the database is in use" when performing a Microsoft SQL 2008 database restore.

by Matt 1. December 2011 10:58

 

This error can be a real pain when having to restore backup files between different servers, or even your local machine.  One way to be sure to fix this is to just bulldoze your way through the issue.

But – make sure you aren’t stepping on anyone else’s toes when doing this!

First, make sure you’re using [master] database, or you might be the connection that’s using the database.

USE MASTER
GO

Then find out which process is using the database.

exec sp_who2

Then ask the user nicely to log off.  Or, just kill the connection outright Smile

KILL {spid}
GO

Now restore your database.

Categories: SQL Server