DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How to Recover a Deleted Table in a SQL Server Database
  • Creating a Hybrid Disaster Recovery Solution Utilizing Availability Group and Log-Shipping
  • Restoring the MS SQL Server Database in Easy Steps
  • How To Convert MySQL Database to SQL Server

Trending

  • Mocking Kafka for Local Spring Development
  • Exactly-Once Processing: Myth vs Reality
  • Introduction to Tactical DDD With Java: Steps to Build Semantic Code
  • From APIs to Actions: Rethinking Back-End Design for Agents
  1. DZone
  2. Data Engineering
  3. Databases
  4. How To Fix SQL Database Restore Failed, Database Is in Use

How To Fix SQL Database Restore Failed, Database Is in Use

This article talks about the “Restore of database failed, Database is in use” and solutions to resolve this error and resolve it through third-party software.

By 
Priyanka Chauhan user avatar
Priyanka Chauhan
·
Oct. 12, 23 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
43.5K Views

Join the DZone community and get the full member experience.

Join For Free

When restoring the SQL Server database from backup, you may encounter the “Restore of database failed, Database is in use” error message. This happens if someone is running queries or if you have an active connection or active windows running on the database to be restored.

The complete error message looks like the following:

MS SQL
 
Msg 3101, Level 16, State 1, Line 2
Exclusive access could not be obtained because the database is in use.


Error message in database restored fail

The above message means that you cannot restore the database when it is being used.

Now, let’s see how to fix the ‘database is in use’ error in SQL Server.

How To Fix SQL Database Restore Failed, Database Is in Use Error

Below, we will mention different methods to solve this error.

Method 1: Close the Connections

As mentioned above, this error can occur if there are any active connections to the database. So, you can close any existing connections to the database to fix the issue. Here are the steps:

  • Open SQL Server Management Studio (SSMS). 
  • In Object Explorer, right-click the Databases option and then click Restore Database.
  • In the Restore Database window, go to the Options page and check the Close existing connections to destination database option.
  • Open SQL Server Management Studio (SSMS). 
  • In Object Explorer, right-click the Databases option and then click Restore Database.
  • In the Restore Database window, go to the Options page and check the Close existing connections to destination database option.

Close existing connections to destination database


This will close all the existing connections.

Alternatively, you can use the following T-SQL commands to close the existing connections.

MS SQL
 
USE [master]

ALTER DATABASE [stellardb] SET SINGLE_USER WITH ROLLBACK IMMEDIATE

BACKUP LOG [stellardb] TO  DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\Backup\stellardb_LogBackup_2023-09-17_08-28-49.bak' WITH NOFORMAT, NOINIT,  NAME = N'stellardb_LogBackup_2023-09-17_08-28-49', NOSKIP, NOREWIND, NOUNLOAD,  NORECOVERY ,  STATS = 5

RESTORE DATABASE [stellardb] FROM  DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\Backup\stellardb.bak' WITH   FILE = 1,   NOUNLOAD,  STATS = 5

ALTER DATABASE [stellardb] SET MULTI_USER

 GO


Once all the SQL server connections are closed, you can proceed with the restore operation.

Method 2: Set the Database to Single-User Mode

You can also set the database to single-user mode to close the connections. In this, you need to:

  1. Set the database to single-user mode and disconnect the other users.
  2. Then, set it again to multi-user mode if necessary.

You can use the following code in T-SQL to set the database to single-user mode:

MS SQL
 
ALTER DATABASE [stellardb]
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO


This will disconnect all the existing connections except one.

To set the database again to multi-user mode, you can use the below code:

MS SQL
 
ALTER DATABASE [stellardb] SET MULTI_USER


Method 3: Bring the Database Offline

To resolve the issue, you can also take the database offline. This will disconnect the database and remove all the existing connections. To do so, follow the below instructions:

  • Open SSMS and go to Object Explorer. 
  • Under the Databases option, right-click the database you want to take offline.
  • Then, select Tasks > Take Offline.update database offline

Once the database is offline, it will remove all the existing connections.

After that, you can bring it online. To do so, right-click the database and select Tasks > Bring Online.

learn how to change bring online

Method 4: Restart the SQL Service

You can also restart the SQL service to disconnect the users and close all the connections in all the databases. The entire SQL Server will be restarted.

To do this, in SSMS, right-click your SQL Server in the Object Explorer and select the restart option.

Note: Use this method only if the previous methods fail. 

Restart the method

What To Do if the Issue Still Persists

If the issue is still not resolved, it means that the database or backup file is corrupted. In such a case, you can use a third-party SQL repair software to fix the backup file and restore the database. The Technician edition of the software can repair the corrupt database files and also the backups. It can restore all the database objects from the backup and save them in a New Database, Live Database, or Other Formats (like CSV, HTML, and XLS). The software supports all SQL Server backup types created in any SQL Server version.   

You need to install Stellar Repair for MS SQL Technician and select the Extract from MS SQL Backup option. Then, follow some simple steps to fix your SQL Server backup file.

Extract SQL Server backup file

Conclusion 

The error “Database is in use” appears when restoring the SQL Server database from the backup. As indicated in the message, if the database is in use, you cannot restore it. To fix this error, you can follow the solutions mentioned in this article. You need to check and close existing connections before trying to restore the database. If the manual solutions fail, then there might be a chance that the backup file is corrupted.

Database Error message Microsoft SQL Server Use error sql

Opinions expressed by DZone contributors are their own.

Related

  • How to Recover a Deleted Table in a SQL Server Database
  • Creating a Hybrid Disaster Recovery Solution Utilizing Availability Group and Log-Shipping
  • Restoring the MS SQL Server Database in Easy Steps
  • How To Convert MySQL Database to SQL Server

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook