Skip to content

Commit df00d7f

Browse files
authored
Merge pull request #3560 from Rob-Hague/fullstopat
sp_DatabaseRestore: fix backup file selection for @StoPAt and non-split full backups
2 parents 36f4e72 + e68196a commit df00d7f

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

sp_DatabaseRestore.sql

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -690,13 +690,15 @@ BEGIN
690690
END;
691691

692692
-- Find latest full backup
693-
SELECT @LastFullBackup = MAX(BackupFile)
693+
-- Get the TOP record to use in "Restore HeaderOnly/FileListOnly" statement as well as Non-Split Backups Restore Command
694+
SELECT TOP 1 @LastFullBackup = BackupFile, @CurrentBackupPathFull = BackupPath
694695
FROM @FileList
695696
WHERE BackupFile LIKE N'%.bak'
696697
AND
697698
BackupFile LIKE N'%' + @Database + N'%'
698699
AND
699-
(@StopAt IS NULL OR REPLACE( RIGHT( REPLACE( @LastFullBackup, RIGHT( @LastFullBackup, PATINDEX( '%_[0-9][0-9]%', REVERSE( @LastFullBackup ) ) ), '' ), 16 ), '_', '' ) <= @StopAt);
700+
(@StopAt IS NULL OR REPLACE( RIGHT( REPLACE( BackupFile, RIGHT( BackupFile, PATINDEX( '%_[0-9][0-9]%', REVERSE( BackupFile ) ) ), '' ), 16 ), '_', '' ) <= @StopAt)
701+
ORDER BY BackupFile DESC;
700702

701703
/* To get all backups that belong to the same set we can do two things:
702704
1. RESTORE HEADERONLY of ALL backup files in the folder and look for BackupSetGUID.
@@ -736,11 +738,6 @@ BEGIN
736738
SET @FileListParamSQL += N')' + NCHAR(13) + NCHAR(10);
737739
SET @FileListParamSQL += N'EXEC (''RESTORE FILELISTONLY FROM DISK=''''{Path}'''''')';
738740

739-
-- get the TOP record to use in "Restore HeaderOnly/FileListOnly" statement as well as Non-Split Backups Restore Command
740-
SELECT TOP 1 @CurrentBackupPathFull = BackupPath, @LastFullBackup = BackupFile
741-
FROM @FileList
742-
ORDER BY REPLACE( RIGHT( REPLACE( BackupFile, RIGHT( BackupFile, PATINDEX( '%_[0-9][0-9]%', REVERSE( BackupFile ) ) ), '' ), 16 ), '_', '' ) DESC;
743-
744741
SET @sql = REPLACE(@FileListParamSQL, N'{Path}', @CurrentBackupPathFull + @LastFullBackup);
745742

746743
IF @Debug = 1

0 commit comments

Comments
 (0)