Click here to Skip to main content
15,886,806 members
Articles / All Topics

Fixing database Is In Use Error While Restoring Database from Backup

Rate me:
Please Sign up or sign in to vote.
5.00/5 (10 votes)
30 Nov 2011CPOL1 min read 104.1K   8   5
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)


Written By
Chief Technology Officer
Bangladesh Bangladesh
I am a Software Engineer and Microsoft .NET technology enthusiast. Professionally I worked on several business domains and on diverse platforms. I love to learn and share new .net technology and my experience I gather in my engineering career. You can find me from here

Personal Site
Personal Blog
FB MS enthusiasts group
About Me

Comments and Discussions

 
QuestionFixing database Is In Use Error While Restoring Database from Backup Pin
Member 1496894318-Feb-21 22:15
Member 1496894318-Feb-21 22:15 
GeneralMy vote of 5 Pin
Krekkon10-Apr-14 1:19
Krekkon10-Apr-14 1:19 
Questionif its possilbe i would give 10 Pin
write2varun14-Sep-13 3:03
professionalwrite2varun14-Sep-13 3:03 
GeneralMy vote of 5 Pin
Rubaba11-Dec-11 0:32
Rubaba11-Dec-11 0:32 
GeneralRe: My vote of 5 Pin
Shahriar Iqbal Chowdhury/Galib12-Dec-11 8:18
professionalShahriar Iqbal Chowdhury/Galib12-Dec-11 8:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.