SQL语句还原数据库并移动文件到指定路径

用SQL语句还原数据库时如果不指定数据库文件的存储路径,则默认把数据文件和日志文件存放到与原数据库相同的文件路径中,这样可能会产生错误,比如执行下面的语句:

 

restore database SmartDev_11_28
from disk='D:\Backup\SmartDev_20170904010000_413.bak'

 

得到如下的错误信息:

 

想要在语句中指定文件路径需要用with move,

restore database 数据库名

from disk='备份文件路径'

with move '逻辑文件名' to '还原后数据库数据文件存放路径'

       move '逻辑日志名' to '还原后数据库日志文件存放路径'

这里的逻辑文件名和逻辑日志名可以通过下面的语句来查看:

 

restore filelistonly 
from disk='D:\Backup\SmartDev_20170904010000_413.bak'

 

 

还原代码如下:

 

restore database SmartDev_11_28
from disk='D:\Backup\SmartDev_20170904010000_413.bak'
with recovery,
  move 'Intertek.Smart.Dev' to 'D:\DataBase\SZDBF8NBD2\SmartDev_11_28\SmartDev_11_28.mdf',
  move 'FileStreamGroup_4D837C1B' to 'D:\DataBase\SZDBF8NBD2\SmartDev_11_28\SmartDev_FileStreamGroup_4D837C1B.mdf',
  move 'Intertek.Smart.Dev_log' to 'D:\DataBase\SZDBF8NBD2\SmartDev_11_28\SmartDev_11_28.ldf',
  move 'filestream_data' to 'D:\DataBase\SZDBF8NBD2\SmartDev_11_28\filestream_data',
  move 'FileStream1' to 'D:\DataBase\SZDBF8NBD2\SmartDev_11_28\FileStream1'

 

还原数据库成功。

 

还原后数据库各文件存放在该目录下。

 

posted on 2017-11-30 09:35  石公子  阅读(3039)  评论(0编辑  收藏  举报