前段时间我就做过类似程序的!
必须说明的是:在恢复差异备份时,必须恢复最后一次的完整备份!!(切记)
而且下面的两个语句必须同时执行,即放在一个事务中。
restore database test from disk='c:\test.bak' with file=离你要恢复的差异备份最近一次的完整备份号,
norecovery
restore database test from disk='c:\test.bak' with file=你要还原的差异备份号,recovery |
具体的备份号可以从下面得到:(你可以认真研究一下backupfile,backupset,backmediaset,backupmediafamily几个表,可以发现规律)
select backup_start_date as 备份时间,position as 备份号,
case type when 'D' then '完整备份' when 'I' then '差异备份' end as 备份类型
from msdb..backupset where database_name='test'
and media_set_id in
(select distinct media_set_id from msdb..backupmediafamily
where physical_device_name='c:\test.bak')
order by position |
如果还不行的话,可以给我留言~
---执行下面的序列:
create database test
go
use test
go
create table test..t(a int)
insert test..t select 1
backup database test to disk='c:\test.bak'
insert test..t select 2
backup database test to disk='c:\test.bak'
insert test..t select 3
insert test..t select 4
backup database test to disk='c:\test.bak' with differential
delete test..t
go |
--下面开始恢复:
restore database test from disk='c:\test.bak' with file=2,norecovery --对应你最后一次的完整备份
restore database test from disk='c:\test.bak' with file=3 --对应你要还原的差异备份
go
select * from test |