From 7d3add4f4a576b8522401f3f645fdbd10426b683 Mon Sep 17 00:00:00 2001 From: Lukasz Biegus Date: Wed, 31 Jan 2024 17:30:53 +0000 Subject: [PATCH] Add FileNamePrefix parameter to sp_DatabaseRestore --- README.md | 1 + sp_DatabaseRestore.sql | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b2446796..5894f3b2 100644 --- a/README.md +++ b/README.md @@ -513,6 +513,7 @@ Parameters include: * @BackupPathFull - typically a UNC path like '\\\\FILESERVER\BACKUPS\SQL2016PROD1A\LogShipMe\FULL\' that points to where the full backups are stored. Note that if the path doesn't exist, we don't create it, and the query might take 30+ seconds if you specify an invalid server name. * @BackupPathDiff, @BackupPathLog - as with the Full, this should be set to the exact path where the differentials and logs are stored. We don't append anything to these parameters. * @MoveFiles, @MoveDataDrive, @MoveLogDrive - if you want to restore to somewhere other than your default database locations. +* @FileNamePrefix - Prefix to add to the names of all restored files. Useful when you need to restore different backups of the same database into the same directory. * @RunCheckDB - default 0. When set to 1, we run Ola Hallengren's DatabaseIntegrityCheck stored procedure on this database, and log the results to table. We use that stored proc's default parameters, nothing fancy. * @TestRestore - default 0. When set to 1, we delete the database after the restore completes. Used for just testing your restores. Especially useful in combination with @RunCheckDB = 1 because we'll delete the database after running checkdb, but know that we delete the database even if it fails checkdb tests. * @RestoreDiff - default 0. When set to 1, we restore the ncessary full, differential, and log backups (instead of just full and log) to get to the most recent point in time. diff --git a/sp_DatabaseRestore.sql b/sp_DatabaseRestore.sql index 07273a2b..ab908d2e 100755 --- a/sp_DatabaseRestore.sql +++ b/sp_DatabaseRestore.sql @@ -38,7 +38,8 @@ ALTER PROCEDURE [dbo].[sp_DatabaseRestore] @Help BIT = 0, @Version VARCHAR(30) = NULL OUTPUT, @VersionDate DATETIME = NULL OUTPUT, - @VersionCheckMode BIT = 0 + @VersionCheckMode BIT = 0, + @FileNamePrefix NVARCHAR(260) = NULL AS SET NOCOUNT ON; SET STATISTICS XML OFF; @@ -791,7 +792,7 @@ BEGIN WHEN Type = 'L' THEN @MoveLogDrive WHEN Type = 'S' THEN @MoveFilestreamDrive WHEN Type = 'F' THEN @MoveFullTextCatalogDrive - END + CASE + END + COALESCE(@FileNamePrefix, '') + CASE WHEN @Database = @RestoreDatabaseName THEN REVERSE(LEFT(REVERSE(PhysicalName), CHARINDEX('\', REVERSE(PhysicalName), 1) -1)) ELSE REPLACE(REVERSE(LEFT(REVERSE(PhysicalName), CHARINDEX('\', REVERSE(PhysicalName), 1) -1)), @Database, SUBSTRING(@RestoreDatabaseName, 2, LEN(@RestoreDatabaseName) -2)) END AS TargetPhysicalName,