Restore Backup Database Failed Because Database Is in Use

How to restore backup database while the existing database is in use

Sometimes, backing up and restoring database is not so straight forward. I used to get the issue of the current database is in use and cannot restore the backup database. But my life is saved when I found the following scripts. 🙂

USE MASTER
GO

ALTER DATABASE MyDatabase
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
Go

RESTORE DATABASE MyDatabase
FROM DISK = N'[file address]'
Go

ALTER DATABASE MyDatabase
SET MULTI_USER with ROLLBACK IMMEDIATE
Go

 

Advertisements

Leave a comment

Filed under Failed to Restore Backup Database, Microsoft SQL Server

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s