56

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.

Is there a better option to do that?

1
  • It's the easiest - and sometimes only - method I found that works consistently. Commented Dec 3, 2019 at 20:20

9 Answers 9

86

Use this code to destory all existing connections before restore:

USE master;
GO
ALTER DATABASE YourDB
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
ALTER DATABASE YourDB
SET MULTI_USER;
GO
Sign up to request clarification or add additional context in comments.

3 Comments

Just close all connections you opened, I mean close all windows you opened for New Query (and maybe change user default database to the master db).
@Mohammadlm71 I wasn't aware that the query window acts as an active connection. That's weird actually
FWIW to anybody else. It was the query windows for me as well. @MuhammadMusavi - thanks.
50

Select the backup file to restore and go to options to select "Close existing connections to destination database" and click ok

Screenshot

6 Comments

Other approaches might work or it might not. Pretty sure this approach works.
I don't have this options available ... :\
This didn't work for me. The destination database didn't even exist yet.
this works for me, because some of my team are using the db. have to forcibly close any existing connection to the db i want to restore
well this didn't work for me, and now my DB is stuck in single user mode
|
7

Restarting the SQL Service solves my problems.

3 Comments

This isn't a particularly satisfying solution, but it worked for me too where the others didn't.
This is the only solution in some situations
This worked straight
2

What worked for me was:

  1. right click on your db -> Tasks -> Take Offline
  2. restore db
  3. then, to bring it back online, right click on your db -> Tasks -> Bring Online

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
1

The problem is that your database is currently being used by others and probably has open connections. What you would need to do is close all the existing connections to it, and then restore your backup.

Here is a link showing how to kill active connections: http://wiki.lessthandot.com/index.php/Kill_All_Active_Connections_To_A_Database

Comments

1

USe activity monitor and filter on the DB you are wanting to restore. Then check with the user that is using it and make sure it is ok to resotre the DB. Then run the following query.

    USE Master

    KILL <session_id from activity monitor>

Comments

1

Shut down the database engine and start it again. Do not just restart because it doesn't stop the instance completely. The error may be fake but check your environment to confirm there are no running applications that automatically reconnect after losing a database connection.

I have never found an answer to explain why restoring from a file to a new database is considered "in use".

Sometimes, @Serjik's answer to take the origin database down to single-user and back has worked for me. About 80% of the time, I have to shut it all down and start it up again before I can restore through SQL Server Management Server.

Comments

0

If still not solved try to execute following

Restore database YOUR_DB_NAME with recovery

Comments

-1

try restoring using SQL

RESTORE DATABASE my_new_database FROM disk = 'E:\path\name.bak'

sometimes I had these issues with UI but worked fine with SQL statement. you need to kill all the open process before restoring, sometimes the process can not close themselves due to some issues with your windows or SQL installation files.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.