Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / All-Topics

Fixing database Is In Use Error While Restoring Database from Backup

5.00/5 (10 votes)
30 Nov 2011CPOL1 min read 104.7K  
Fixing database is in use error while restoring database from backup

Background

When performing a full restore over an existing database, SQL Backup sometimes reports that the database cannot be restored because it is currently in use, like shown below:

Image 1

This error shows not only during restore backup, also for drop database or alter database as well.

Workaround

In this situation, database administrator needs to disconnect all the connected users from a SQL Server Database to get exclusive access of the database. In the situation where a full restore is blocked because users are currently connected to the database, the best solution to this problem is to take the database offline prior to running the restore, as this is the easiest way to kill all connections to the database.

Before Restore Attempt

Database administrator can execute the below mentioned TSQL command to get a Single User access to a database. SINGLE_USER WITH ROLLBACK IMMEDIATE command is used so that only one user can connect to the database at a time.

SQL
use master
alter database DB_NAME set offline with rollback immediate;

After Restore

Database administrator can execute the below mentioned TSQL command to give Multiple User access to a database. MULTI_USER command is used by any number of users who have rights to connect to the database that will be able to connect to the database.

SQL
use master
alter database DB_NAME set online with rollback immediate;

So now all authenticated database users can connect to the database again.

Image 2 Image 3 Image 4 Image 5 Image 6 Image 7 Image 8 Image 9

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)