Skip to content

Commit 27d9fe9

Browse files
authored
Merge pull request #3049 from daniel70/issue_3038
Changed the way the @StoPAt param is used.
2 parents 13cf617 + 5940a41 commit 27d9fe9

File tree

1 file changed

+57
-13
lines changed

1 file changed

+57
-13
lines changed

sp_DatabaseRestore.sql

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,19 +1271,6 @@ BEGIN
12711271

12721272
END
12731273

1274-
IF (@StopAt IS NOT NULL)
1275-
BEGIN
1276-
1277-
IF @Execute = 'Y' OR @Debug = 1 RAISERROR('@OnlyLogsAfter is NOT NULL, deleting from @FileList', 0, 1) WITH NOWAIT;
1278-
1279-
DELETE fl
1280-
FROM @FileList AS fl
1281-
WHERE BackupFile LIKE N'%.trn'
1282-
AND BackupFile LIKE N'%' + @Database + N'%'
1283-
AND REPLACE( RIGHT( REPLACE( fl.BackupFile, RIGHT( fl.BackupFile, PATINDEX( '%_[0-9][0-9]%', REVERSE( fl.BackupFile ) ) ), '' ), 16 ), '_', '' ) > @StopAt;
1284-
1285-
END
1286-
12871274
-- Check for log backups
12881275
IF(@BackupDateTime IS NOT NULL AND @BackupDateTime <> '')
12891276
BEGIN
@@ -1309,6 +1296,63 @@ IF (@LogRecoveryOption = N'')
13091296
SET @LogRecoveryOption = N'NORECOVERY';
13101297
END;
13111298

1299+
IF (@StopAt IS NOT NULL)
1300+
BEGIN
1301+
1302+
IF @Execute = 'Y' OR @Debug = 1 RAISERROR('@OnlyLogsAfter is NOT NULL, deleting from @FileList', 0, 1) WITH NOWAIT;
1303+
1304+
IF LEN(@StopAt) <> 14 OR PATINDEX('%[^0-9]%', @StopAt) > 0
1305+
BEGIN
1306+
RAISERROR('@StopAt parameter is incorrect. It should contain exactly 14 digits in the format yyyyMMddhhmmss.', 16, 1) WITH NOWAIT;
1307+
RETURN
1308+
END
1309+
1310+
IF ISDATE(STUFF(STUFF(STUFF(@StopAt, 13, 0, ':'), 11, 0, ':'), 9, 0, ' ')) = 0
1311+
BEGIN
1312+
RAISERROR('@StopAt is not a valid datetime.', 16, 1) WITH NOWAIT;
1313+
RETURN
1314+
END
1315+
1316+
-- Add the STOPAT parameter to the log recovery options but change the value to a valid DATETIME, e.g. '20211118040230' -> '20211118 04:02:30'
1317+
SET @LogRecoveryOption += ', STOPAT = ''' + STUFF(STUFF(STUFF(@StopAt, 13, 0, ':'), 11, 0, ':'), 9, 0, ' ') + ''''
1318+
1319+
IF @BackupDateTime = @StopAt
1320+
BEGIN
1321+
IF @Debug = 1
1322+
BEGIN
1323+
RAISERROR('@StopAt is the end time of a FULL backup, no log files will be restored.', 0, 1) WITH NOWAIT;
1324+
END
1325+
END
1326+
ELSE
1327+
BEGIN
1328+
DECLARE @ExtraLogFile NVARCHAR(255)
1329+
SELECT TOP 1 @ExtraLogFile = fl.BackupFile
1330+
FROM @FileList AS fl
1331+
WHERE BackupFile LIKE N'%.trn'
1332+
AND BackupFile LIKE N'%' + @Database + N'%'
1333+
AND REPLACE( RIGHT( REPLACE( fl.BackupFile, RIGHT( fl.BackupFile, PATINDEX( '%_[0-9][0-9]%', REVERSE( fl.BackupFile ) ) ), '' ), 16 ), '_', '' ) > @StopAt
1334+
ORDER BY BackupFile;
1335+
END
1336+
1337+
IF @ExtraLogFile IS NULL
1338+
BEGIN
1339+
DELETE fl
1340+
FROM @FileList AS fl
1341+
WHERE BackupFile LIKE N'%.trn'
1342+
AND BackupFile LIKE N'%' + @Database + N'%'
1343+
AND REPLACE( RIGHT( REPLACE( fl.BackupFile, RIGHT( fl.BackupFile, PATINDEX( '%_[0-9][0-9]%', REVERSE( fl.BackupFile ) ) ), '' ), 16 ), '_', '' ) > @StopAt;
1344+
END
1345+
ELSE
1346+
BEGIN
1347+
DELETE fl
1348+
FROM @FileList AS fl
1349+
WHERE BackupFile LIKE N'%.trn'
1350+
AND BackupFile LIKE N'%' + @Database + N'%'
1351+
AND fl.BackupFile > @ExtraLogFile
1352+
END
1353+
END
1354+
1355+
13121356
-- Group Ordering based on Backup File Name excluding Index {#} to construct coma separated string in "Restore Log" Command
13131357
SELECT BackupPath,BackupFile,DENSE_RANK() OVER (ORDER BY REPLACE( RIGHT( REPLACE( BackupFile, RIGHT( BackupFile, PATINDEX( '%_[0-9][0-9]%', REVERSE( BackupFile ) ) ), '' ), 16 ), '_', '' )) AS DenseRank INTO #SplitLogBackups
13141358
FROM @FileList

0 commit comments

Comments
 (0)