SQL Server Error During Restore Database: “Exclusive access could not be obtained because the database is in use”
Today I needed to restore a database to our test environment but the I got the following error during the restore operation:
Exclusive access could not be obtained because the database is in use
The reason of this SQL error is there are other users currently using the database to be restored. When trying to do a restore, if any other user is in the database you will see this error.
Therefore you should first drop any other connections in this database. sp_who2 procedure can be used to see what connections are using the database you are trying to restore.
To drop connection run these two commands:
alter database your_db_name set offline with rollback immediatealter database your_db_name set online
After this; you can restore your database as usual…