用覆盖sql服务器恢复数据库

这里有一个脚本,试图从前一天进行每日备份,并通过报告数据库进行恢复。 我的问题是如何设置它覆盖文件,所以我不必指定文件名? 当我运行这个时,我遇到了一系列的错误,如下所示。

----Restore Database ALTER DATABASE ReportingDB SET SINGLE_USER GO DECLARE @filename VARCHAR(1000) select @filename = 'F:\DailyBackup\LiveDB_backup_' + cast(datepart(yyyy, getdate()) as varchar(4)) + '' + substring(cast( 100 + datepart(mm, getdate()) as char(3)), 2, 2) + substring(cast( 100 + datepart(day, getdate()) as char(3)), 2, 2) + '0000.bak' select @filename RESTORE DATABASE ReportingDB FROM DISK = @filename WITH REPLACE GO ALTER DATABASE ReportingDB SET MULTI_USER GO (1 row(s) affected) Msg 5133, Level 16, State 1, Line 4 Directory lookup for the file "D:\DB\LiveDB.mdf" failed with the operating system error 21(error not found). Msg 3156, Level 16, State 3, Line 4 File 'LiveDB' cannot be restored to 'D:\DB\LiveDB.mdf'. Use WITH MOVE to identify a valid location for the file. Msg 5133, Level 16, State 1, Line 4 Directory lookup for the file "D:\DB\LiveDB_log.LDF" failed with the operating system error 21(error not found). Msg 3156, Level 16, State 3, Line 4 File 'LiveDB_log' cannot be restored to 'D:\DB\LiveDB_log.LDF'. Use WITH MOVE to identify a valid location for the file. Msg 3119, Level 16, State 1, Line 4 Problems were identified while planning for the RESTORE statement. Previous messages provide details. Msg 3013, Level 16, State 1, Line 4 RESTORE DATABASE is terminating abnormally. 

当我尝试移动时:

将'LiveDB'移动到'F:\ ReportingDB \ ReportingDB.mdf',

MOVE'LiveDB_log'至'F:\ ReportingDB \ ReportingDB_log.ldf'

 (1 row(s) affected) Msg 1834, Level 16, State 1, Line 4 The file 'F:\ReportingDB\ReportingDB.mdf' cannot be overwritten. It is being used by database 'ReportingDB'. Msg 3156, Level 16, State 4, Line 4 File 'LiveDB' cannot be restored to 'F:\ReportingDB\ReportingDB.mdf'. Use WITH MOVE to identify a valid location for the file. Msg 1834, Level 16, State 1, Line 4 The file 'F:\ReportingDB\ReportingDB_log.ldf' cannot be overwritten. It is being used by database 'ReportingDB'. Msg 3156, Level 16, State 4, Line 4 File 'LiveDB_log' cannot be restored to 'F:\ReportingDB\Reporting_log.ldf'. Use WITH MOVE to identify a valid location for the file. Msg 3119, Level 16, State 1, Line 4 Problems were identified while planning for the RESTORE statement. Previous messages provide details. Msg 3013, Level 16, State 1, Line 4 RESTORE DATABASE is terminating abnormally. 

看起来您的原始备份来自位于'D:\DB\LiveDB.mdf'而您现在正试图通过位于'F:\ReportingDB\ReportingDB.mdf'的数据库进行还原,所以您需要MOVE和REPLACE选项的组合。

 RESTORE DATABASE ReportingDB FROM DISK = @filename WITH REPLACE, MOVE 'LiveDB' TO 'F:\ReportingDB\ReportingDB.mdf', MOVE 'LiveDB_log' TO 'F:\ReportingDB\ReportingDB_log.ldf' 

您需要REPLACE选项

 RESTORE DATABASE ReportingDB FROM DISK = @filename, REPLACE 

请参阅文档了解更多详情

Msg 1834,Level 16,State 1,Line 4文件'F:\ ReportingDB \ ReportingDB.mdf'不能被覆盖。 它正在被数据库'ReportingDB'使用。

上述错误可能是由于数据库正在使用,因为您尝试还原。 这将防止数据库被恢复,因此使用以下命令

ALTER DATABASE DATABASENAME SET SINGLE_USER WITH ROLLBACK IMMEDIATE

恢复数据库

请注意,如果您正在运行预定作业,则应该全部在一个步骤中。

这将强制所有其他用户断开(所以他们的报告将失败,如果他们正在运行)

您可能还需要包含以下内容

ALTER DATABASE DATABASENAME SET MULTI_USER

以允许多个连接,尽pipe恢复应该将其设置为与从其备份的数据库相同。