如果您使用脚本方法并且遇到有关 LDF 和 MDF 文件的错误,您可以首先使用以下命令查询备份文件以获取备份集中文件的逻辑名称(和其他详细信息):
-- Queries the backup file for the file list in backup set, where Type denotes
-- type of file. Can be L,D,F or S
-- info: https://docs.microsoft.com/en-us/sql/t-sql/statements/restore-statements-filelistonly-transact-sql
RESTORE FILELISTONLY FROM DISK = 'C:\Temp\DB_backup.bak'
GO
您将获得类似于以下的结果:
然后您可以在查询中使用这些逻辑名称:
-- Script assumes you want MDF and LDF files restored on separate drives. Modify for your scenario
RESTORE DATABASE DB
FROM DISK='C:\Temp\DB_backup.bak'
WITH REPLACE,
MOVE 'DB' TO 'E:\MSSQL\Data\DB.mdf', -- "DB" is the mdf logical name from query above
MOVE 'DB_log' TO 'F:\MSSQL\Logs\DB.ldf'; -- "DB_log" is LDF logical name from query above
更多信息RESTORE FILELISTONLY
可以从 SQL Server 文档中找到。