Skip to content

Instantly share code, notes, and snippets.

@anujbhatt
Created August 31, 2014 10:10
Show Gist options
  • Save anujbhatt/fdaaa9773ee3cd5461ed to your computer and use it in GitHub Desktop.
Save anujbhatt/fdaaa9773ee3cd5461ed to your computer and use it in GitHub Desktop.
Restoring a database in MS SqlServer without using the UI
-- 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