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?
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?
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
Select the backup file to restore and go to options to select "Close existing connections to destination database" and click ok
Restarting the SQL Service solves my problems.
What worked for me was:
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
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>
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.
If still not solved try to execute following
Restore database YOUR_DB_NAME with recovery
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.