Created
August 31, 2014 10:10
-
-
Save anujbhatt/fdaaa9773ee3cd5461ed to your computer and use it in GitHub Desktop.
Restoring a database in MS SqlServer without using the UI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Replace MYDB with the name of your db below | |
-- close existing connections | |
ALTER DATABASE MYDB | |
SET SINGLE_USER | |
WITH ROLLBACK IMMEDIATE | |
GO | |
-- switch to some other db | |
USE master | |
-- determine the mdf, log file names that may be present in the .bak file | |
RESTORE FILELISTONLY | |
FROM DISK = 'C:\path\to\MYDB.bak' | |
GO | |
-- perform the actual restore (mdf and ldf names will come from the step above) | |
RESTORE DATABASE MYDB | |
FROM DISK = 'C:\path\to\MYDB.bak' | |
WITH MOVE 'MYDBDat.mdf' TO N'C:\path\to\MYDBDat.mdf', | |
MOVE 'MYDBLog.ldf' TO N'C:\path\to\MYDBLog.ldf', | |
REPLACE | |
GO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment