Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server: Unable to restore Database, Database is in use by session

I am using SQL Server 2005 and I am having an issue restoring my database I receive this message upon trying to restore my database.

Restore failed. (Microsoft.SqlServer.Express.Smo)

"System.Data.SqlClient.SqlError: RESTORE cannot process database 'AMOD' because it is in use by this session. It is recommended that the master database be used when performing this operation. (Microsoft.SqlServer.Express.Smo)"

I have restarted the program I did not open any tables contained within the database and I still receive this message. I am new to SQL Server and this is my first time doing a restore. I appreciate any help provided.

like image 280
Scott Rinebold Avatar asked Jun 11 '12 14:06

Scott Rinebold


People also ask

Is already open and can only have one user at a time?

It means that you cannot open a database because someone is using it. The 924 error usually happens if you attempt to access a SQL database set to SINGLE_USER mode. Database 'db_name' is already open and can only have one user at a time.


1 Answers

You need to kick all the users out, and make sure you're not in that database too. Assuming you're in Management Studio, you need to change your context to a different database (or switch the database dropdown to a different database) this way, and this will also kick out any other users (which could be you - Object Explorer, Object Explorer Details, other query windows, etc. could all be inadvertently preventing the restore by maintaining a connection to your database):

USE master;
GO
ALTER DATABASE AMOD SET SINGLE_USER WITH ROLLBACK IMMEDIATE;

Once you've finished restoring and the database is ready for use again:

ALTER DATABASE AMOD SET MULTI_USER;
like image 169
Aaron Bertrand Avatar answered Oct 26 '22 07:10

Aaron Bertrand