54

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.
    – Suncat2000
    Commented Dec 3, 2019 at 20:20

9 Answers 9

85

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
3
  • 4
    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). Commented Feb 14, 2018 at 18:44
  • 2
    @Mohammadlm71 I wasn't aware that the query window acts as an active connection. That's weird actually Commented Nov 21, 2018 at 22:16
  • 1
    FWIW to anybody else. It was the query windows for me as well. @MuhammadMusavi - thanks.
    – dgo
    Commented Jun 22, 2022 at 17:29
48

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

Screenshot

6
  • 1
    Other approaches might work or it might not. Pretty sure this approach works.
    – kepung
    Commented Dec 12, 2017 at 22:49
  • 2
    I don't have this options available ... :\ Commented Jun 18, 2018 at 14:06
  • This didn't work for me. The destination database didn't even exist yet.
    – Suncat2000
    Commented Mar 8, 2019 at 21:46
  • 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 Commented Mar 5, 2020 at 1:00
  • well this didn't work for me, and now my DB is stuck in single user mode Commented Jul 8, 2020 at 6:24
7

Restarting the SQL Service solves my problems.

3
  • This isn't a particularly satisfying solution, but it worked for me too where the others didn't. Commented Sep 9, 2016 at 11:30
  • This is the only solution in some situations
    – yeska
    Commented Jul 14, 2017 at 10:10
  • This worked straight
    – Babu James
    Commented Jul 12 at 10:18
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
  • 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.
    – Community Bot
    Commented Nov 30, 2021 at 2:56
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

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>
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.

0

If still not solved try to execute following

Restore database YOUR_DB_NAME with recovery
-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.

Your Answer

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