SQL Server keeps telling me a database is in use when I try to drop it or restore it, but when I run this metadata query:
select * from sys.sysprocesses
where dbid
in (select database_id from sys.databases where name = 'NameOfDb')
It returns nothing.
Sometimes it will return 1 process which is a CHECKPOINT_QUEUE waittype. If I try to kill that process, it won’t let me (cannot kill a non-user process).
Anyone have any idea what’s wrong?
Oded
488k99 gold badges881 silver badges1007 bronze badges
asked Feb 24, 2011 at 13:01
4
i like this script. Do not struggle with killing..
use master
alter database xyz set single_user with rollback immediate
restore database xyz ...
alter database xyz set multi_user
answered Dec 2, 2012 at 18:10
MahmutHAKTANMahmutHAKTAN
6492 gold badges7 silver badges16 bronze badges
1
I was having the same issue when trying to restore a database from a backup. The solution for me was to ensure that I checked «Overrite the existing database(WITH REPLACE)» and «Close existing connections to destination database» in the Options page BEFORE doing the restore. Here is a screenshot below.
answered Aug 22, 2016 at 19:53
Judy007Judy007
5,4364 gold badges44 silver badges67 bronze badges
3
There could be lots of things blocking your database. For example, if you have a query window opened on that database, it would be locked by you. Not counting external accesses, like a web application on IIS.
If you really wanna force the drop on it, check the close existing connections option or try to manually stop SQL Server’s service.
answered Feb 24, 2011 at 13:40
SmurSmur
3,0657 gold badges28 silver badges46 bronze badges
4
Summary:
This blog will describe solutions to fix the ‘SQL database restore failed, database in use’ error. These solutions include disconnecting active connections (users and processes) to the database and by using Stellar Toolkit for MS SQL software to recover SQL database (db) from corrupt db or backup (.bak) file. The solutions apply to MS SQL Server 2019, 2017, 2016, 2014 and lower versions.
Contents
- Reasons behind ‘SQL Database Restore Failed, Database in Use’ Error
- Before We Begin
- How to Fix ‘can’t restore SQL database because it is in use’ Problem?
- What if the problem still persists?
- Conclusion
When trying to restore SQL Server database from backup, it is common to receive an error message that reads as follows:
Restored failed for Server ‘xxx’ (Microsoft.SqlServer.SmoExtended)
Additional Information: System.Data.SqlClient.SqlError: Exclusive access could not be obtained because the database is in use.
Reasons behind ‘SQL Database Restore Failed, Database in Use’ Error
Below are some reasons that could interfere with the restore process and throw the ‘restore of database failed because the database is in use’ error:
- You are connected to the database you are trying to restore.
- While using SQL Server Management Studio (SSMS) to do a database restore, you have more than one window open in it.
- Other users are connected to the master db.
Now, we will discuss solutions to fix the error.
Tip: SQL Server database can be restored from the backup (.bak) file. But, the database restore operation may fail if the .bak file is corrupt. Use Stellar Toolkit for MS SQL software that comes with an efficient SQL backup extractor tool designed to help database administrators recover SQL database from corrupted backup (.BAK) file. The software supports SQL Server 2019, 2017, 2016, 2014, 2012, & older versions.
Before We Begin
Before proceeding with resolving the error – exclusive access could not be obtained because the database is in use, make sure to meet the following prerequisites:
- SQL Server, of any version, must be installed on your system.
- You will need SQL Server Management Studio (SSMS) installed on your computer.
How to Fix ‘can’t restore SQL database because it is in use’ Problem?
When attempting to restore SQL Server db, make sure there are no active connections. If someone is using the database, the restore operation will fail. To resolve the issue, you will need to disconnect the active users. You can do so, by following any of these methods:
NOTE: Before disconnecting the users, use SQL stored procedure ‘sp_who’ to check all users currently using the db. If you find users performing some important tasks, notify those users before disconnecting them. For detailed information on sp_who, refer to this link. If you don’t want to notify users, skip to method 2.
Method 1 – Close the existing connections to the database
To close existing connections to SQL db, follow these steps:
Step 1: Open SSMS and connect to the db.
Step 2: After connecting to the database, Object Explorer panel will appear on the left side of the SSMS window.
Step 3: In Object Explorer panel, right-click Databases, and then select Restore Database.
Step 4: In Restore Database dialog box, do the following:
- Select one of the databases to restore.
- In the left panel, click Options.
Step 5: In Options page, check the checkbox labeled, ‘close existing connections to destination database’.
Once the SQL Server connections are closed, proceed with the restore operation.
Method 2 –Change from multiple-user mode to single-user mode
Changing the multiple-user mode by default to single user mode will disconnect all the connected users. This option can be used, if you want to disconnect all the users without notifying them.
To force users to go offline (i.e. disconnect) from SQL Server, set the db from multiple-user mode to single-user mode by following these steps:
Step 1: Open SSMS, connect to the database.
Step 2: In Object Explorer window, select New Query. Copy and paste the below T-SQL code snippet into the query window, and then click Execute:
USE master; GO ALTER DATABASE AdventureWorks2012 SET SINGLE_USER WITH ROLLBACK IMMEDIATE; GO
Executing the above code will change the database to single-user mode.
Method 3 – Restart the SQL Server Service
You can also disconnect the users by restarting the SQL service. You can restart the service by using SQL Server Configuration Manager, SSMS, services console, or the command line.
NOTE: Use this method as a last resort. That’s because, you may only need to restore a single db, but restarting the server will kill connections to all databases.
Steps to restart the service from SQL Server Configuration Manager are as follows:
Step 1: Browse SQL Server Configuration Manager using any of the following path:
SQL Server 2019 C:WindowsSysWOW64SQLServerManager15.msc
SQL Server 2017 C:WindowsSysWOW64SQLServerManager14.msc
SQL Server 2016 C:WindowsSysWOW64SQLServerManager13.msc
SQL Server 2014 C:WindowsSysWOW64SQLServerManager12.msc
SQL Server 2012 C:WindowsSysWOW64SQLServerManager11.msc
Step 2: In the left pane of SQL Server Configuration Manager window, click SQL Server Services. And in the right pane, right-click SQL Server service, and Stop and Start it.
Step 3: Click OK to exit the SQL Server Configuration Manager.
What if the problem still persists?
If the issue still persists, likely there is a problem with your database or the backup file, used for restoring the database, is corrupt. In that case, use Stellar Toolkit for MS SQL. The software can recover db from a corrupt SQL Server. It can also extract a database – from corrupt backup (.bak) files – that need to be restored.
Stellar Toolkit for MS SQL software can also help you reset lost or forgotten SQL Server Administrator and user passwords. You can read the software review done by MVP from here.
To restore database from corrupt SQL Server backup (.bak) file by using the software, follow these steps:
Step 1: Download, install and launch Stellar Toolkit for MS SQL software.
Step 2: In software’s user interface, select Extract from MS SQL Backup.
Step 3: In Stellar Backup Extractor for MS SQL window, click Select File to choose the .bak file.
NOTE: Choose ‘Search in Folder’ option, if you do not know the file location.
Step 4: After selecting the .bak file, click Scan.
Step 5: The BackupSet window appears with details of all the backups.
Step 6: Choose the .bak file you want to recover from the Backup Type list, and then click Next to proceed with the scanning process.
Step 7: Once scanning is complete, a dialog box appears displaying the number of total records available in the backup file.
Step 8: The software shows a preview of the database records.
Step 9: To save the recovered .bak file, click Save on File menu.
Step 10: In the window that pops-up, choose MSSQL under Save As, and then click Browse to select the location to save the recovered file. Click OK.
Step 11: Choose New Database or Live Database under Saving Options. Next, specify details required in Connect to Server section, and then click Connect.
Step 12: Click OK when the ‘Recovery process successfully completed’ message appears.
The recovered file will get saved in the selected location.
You can watch the complete video from here:
Conclusion
This blog explained how to fix the SQL database restore failed, database in use problem. You can disconnect active users by closing the existing connections or by changing from multiple-user mode to single-user mode. Or, disconnect all the users by restarting the SQL Server service. But, if you still have issues restoring the db, Stellar SQL Database Toolkit can come in handy. It helps resolving the issue by repairing the corrupt SQL db or by recovering the SQL Server backup file.
About The Author
Priyanka Chauhan
Priyanka is a technology expert working for key technology domains that revolve around Data Recovery and related software’s. She got expertise on related subjects like SQL Database, Access Database, QuickBooks, and Microsoft Excel. Loves to write on different technology and data recovery subjects on regular basis. Technology freak who always found exploring neo-tech subjects, when not writing, research is something that keeps her going in life.
Error Details — SQL Database Is in Use
«I’m using SQL Server 2008 R2, I need to restore a database from a .bak file. There is always an error that the database is in use until I restart the SQL Server service.»
This is a comment on StackOverflow. As you see, when we try to restore a database in Microsoft SQL Server Management Studio (SSMS), it does not work because some other users are currently using the database, too. Usually, the full error message is like this.
Restored failed for Server
Additional Information: System.Data.SqlClient.SqlError: Exclusive access could not be obtained because the database is in use.
Priyanka Chauhan
Priyanka is a technology expert working for key technology domains that revolve around Data Recovery and related software’s. She got expertise on related subjects like SQL Database, Access Database, QuickBooks, and Microsoft Excel. Loves to write on different technology and data recovery subjects on regular basis. Technology freak who always found exploring neo-tech subjects, when not writing, research is something that keeps her going in life.