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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

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

  • How to Create a Successful API Ecosystem
  • A Complete Guide to Modern AI Developer Tools
  • Intro to RAG: Foundations of Retrieval Augmented Generation, Part 2
  • FIPS 140-3: The Security Standard That Protects Our Federal Data
  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
31.4K 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
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!