用 SQL 语句还原数据库(with move 选项用法)

1. 查看 SQL Server 2000 中 Northwind 数据库文件的逻辑文件名(logical file name)和物理文件路径(operation system file name):

use Nothwind
go

select name, filename, * from dbo.sysfiles
name            filename
--------------  ------------------------------------------------------------------
Northwind       d:/program files/microsoft sql server/mssql$sqla/data/northwnd.mdf
Northwind_log   d:/program files/microsoft sql server/mssql$sqla/data/northwnd.ldf

2. 备份 SQL Server 2000.Northwind 数据库:

backup database Northwind to disk = 'c:/Northwind.bak'

3. 在 SQL Server 2005 Instance 中还原 Northwind 数据库:

use master
go
restore database Northwind from disk = 'c:/Northwind.bak'
用普通方式 restore 的话,SQL Server 会报出一大堆的错误信息:

Msg 3634, Level 16, State 1, Line 1
The operating system returned the error '32(error not found)'
while attempting 'RestoreContainer::ValidateTargetForCreation'
on 'd:/Program Files/Microsoft SQL Server/MSSQL$SQLA/data/northwnd.mdf'.

Msg 3156, Level 16, State 8, Line 1
File 'Northwind' cannot be restored to
'd:/Program Files/Microsoft SQL Server/MSSQL$SQLA/data/northwnd.mdf'.
Use WITH MOVE to identify a valid location for the file.

Msg 3634, Level 16, State 1, Line 1
The operating system returned the error '32(error not found)'
while attempting 'RestoreContainer::ValidateTargetForCreation'
on 'd:/Program Files/Microsoft SQL Server/MSSQL$SQLA/data/northwnd.ldf'.

Msg 3156, Level 16, State 8, Line 1
File 'Northwind_log' cannot be restored to
'd:/Program Files/Microsoft SQL Server/MSSQL$SQLA/data/northwnd.ldf'.
Use WITH MOVE to identify a valid location for the file.

Msg 3119, Level 16, State 1, Line 1
Problems were identified while planning for the RESTORE statement. Previous messages provide details.

Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.

4. 在 SQL Server 2005 中还原 Northwind 数据库正确方法:restore with move。

SQL Server 2005 中数据文件所在目录为:d:/microsoft sql server/mssql.1/mssql/data/
restore database Northwind from disk = 'c:/Northwind.bak'
with move 'Northwind'     to 'd:/microsoft sql server/mssql.1/mssql/data/Northwind.mdf'
    ,move 'Northwind_log' to 'd:/microsoft sql server/mssql.1/mssql/data/Northwind.ldf'

Northwind 数据库在 SQL Server 2005 中顺利还原。

注意,在 SQL Server 2005 中还原 Northwind 的时候,并不需要首先创建一个同名的 Northwind 数据库,而是直接进行 restore。

另外的一个问题是,如果客户给你一个 Northwind.bak 备份文件,让你在自己的机器上 restore。那么如何来确定 with move 中的逻辑文件名呢?一个方法是向客户咨询获取,二是使用 SQL Server restore filelistonly 来查看。

5. 使用 SQL Server restore filelistonly 命令来查看逻辑文件名

从 SQL Server restore filelistonly 命令结果中可以获取很多信息,下面仅列出 LogicalName and PhysicalName。

restore filelistonly from disk='c:/Northwind.bak'
LogicalName       PhysicalName
---------------  ------------------------------------------------------------------
Northwind        d:/Program Files/Microsoft SQL Server/MSSQL$SQLA/data/northwnd.mdf
Northwind_log    d:/Program Files/Microsoft SQL Server/MSSQL$SQLA/data/northwnd.ldf
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值