Skip to content

Instantly share code, notes, and snippets.

@huyttq
Last active July 11, 2018 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huyttq/6679466 to your computer and use it in GitHub Desktop.
Save huyttq/6679466 to your computer and use it in GitHub Desktop.
Restore MS SQL Server DB with replace
use [master]
Declare @BackupFileLocation varchar(max)
Declare @BackupMDFFileName varchar(max)
Declare @BackupLogFileName varchar(max)
Declare @ImportTesterMDFFileName varchar(max)
Declare @ImportTesterLogFileName varchar(max)
set @BackupFileLocation =
N'\\file-path\bk.bak'
set @BackupLogicalMDFFileName = N'LogicalFileName'
set @BackupLogicalLogFileName = N'LogicalFileName_log'
set @ImportTesterMDFFileName = N'C:\file-path\Test.mdf'
set @ImportTesterLogFileName = N'C:\file-path\Test_log.ldf'
--Begin Try
use [Master]
RESTORE DATABASE [Test]
FROM DISK = @BackupFileLocation WITH FILE = 1,
MOVE @BackupLogicalMDFFileName TO @ImportTesterMDFFileName,
MOVE @BackupLogicalLogFileName TO @ImportTesterLogFileName,
NOUNLOAD, REPLACE, STATS = 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment