RESTORE DATABASE Status_backup_2015_05_31_230001_686899
FROM DISK = 'E:\Restore\Status_backup_2015_05_31_230001_686899.bak'
WITH FILE = 1,
MOVE 'StatusBackup_Data' TO 'E:\Restore\Files\Status_backup_2015_05_31_230001_686899.mdf',
MOVE 'StatusBackup_Log' TO 'E:\Restore\Files\Status_backup_2015_05_31_230001_686899.ldf',
NOUNLOAD, REPLACE, STATS = 1
GO
我有以下脚本从备份恢复数据库。还有9个带有日期戳的.bak文件
Status_backup_2015_10_31_230001_754565
Status_backup_2015_09_31_230001_865643
Status_backup_2015_08_31_230012_967686
Status_backup_2015_07_31_230001_676760
Status_backup_2015_06_31_230001_665455
Status_backup_2015_04_31_230001_355656
Status_backup_2015_03_31_230001_246754
Status_backup_2015_02_31_230001_646457
Status_backup_2015_01_31_230001_975644
我需要将所有这些作为循环包含在上面的脚本中。我想添加,但没有得到正确的脚本包括。请有人建议我如何添加剩余的.bak文件。
感谢。
答案 0 :(得分:0)
尝试使用这种基于游标的方法:
CREATE TABLE #restoreFiles(
backupfile VARCHAR(100))
INSERT INTO #restoreFiles (backupfile)
VALUES ('Status_backup_2015_10_31_230001_754565'),
('Status_backup_2015_09_31_230001_865643'),
('Status_backup_2015_08_31_230012_967686'),
('Status_backup_2015_07_31_230001_676760'),
('Status_backup_2015_06_31_230001_665455'),
('Status_backup_2015_04_31_230001_355656'),
('Status_backup_2015_03_31_230001_246754'),
('Status_backup_2015_02_31_230001_646457'),
('Status_backup_2015_01_31_230001_975644')
DECLARE @fileToRestore VARCHAR(100), @backupLocation varchar(500), @mdfLocation varchar(500), @ldfLocation varchar(500)
DECLARE restorecursor CURSOR FOR
SELECT backupfile FROM #restoreFiles
OPEN restorecursor
FETCH NEXT FROM restorecursor
INTO @fileToRestore
WHILE @@FETCH_STATUS = 0
BEGIN
SET @backupLocation = 'E:\Restore\' + @fileToRestore + '\' + @fileToRestore +'.bak'
SET @mdfLocation = 'E:\Restore\Files\' + @fileToRestore + '.mdf'
SET @ldfLocation = 'E:\Restore\Files\' + @fileToRestore + '.ldf'
RESTORE DATABASE @fileToRestore
FROM DISK = @backupLocation
WITH FILE = 1,
MOVE 'StatusBackup_Data' TO @mdfLocation,
MOVE 'StatusBackup_Log' TO @ldfLocation,
NOUNLOAD, REPLACE, STATS = 1
FETCH NEXT FROM restorecursor
INTO @fileToRestore
END
CLOSE restorecursor
DEALLOCATE restorecursor