怎样用触发器对SQL Server进行数据备份(转载)_温故知新循序渐进_百度空间

用触发器对SQL Server进行数据备份:

1、建立测试数据表:

一个用于插入数据,test3
另一个作为备份,test3_bak
create table test3(id int primary key not null
identity(1,1),uname varchar(20),uage int);
create table test3_bak(id int primary key not
null identity(1,1),bid int,uname varchar(20),
uage int,active char(1));

2、编写备份用的触发器,只有更新或者是插入的时候才触发

alter trigger test3_bak_insert_update
on test3
for insert,update
as
declare @id int
declare @uname varchar(20)
declare @uage int
begin
select @id=id,@uname=uname,@uage=uage from inserted
if @id<>0
begin
update test3_bak set active='0' where bid=@id
insert into test3_bak(bid,uname,uage,active)
values(@id,@uname,@uage,'1')
end
end

--insert,update,
alter trigger test4_bak_insert_update
on test4
for delete
as
declare @id int
declare @uname varchar(20)
declare @uage int
begin
--select @id=id,@uname=uname,@uage=uage from inserted
select @uage = uage from deleted
if @id<>0
begin
update test4_bak set active='0' where bid=@id
--  insert into test4_bak(bid,uname,uage,active)
--   values(@id,@uname,@uage,'1')
delete from test4_bak where uage = @uage
end
end

3、测试数据:

insert into test3(uname,uage) values('FLB',20)
insert into test3(uname,uage) values('FLB1',21)
insert into test3(uname,uage) values('FLB2',22)

update test3 set uage=100 where id=27
delete from test3 where id=20

4、可自己采用下面的查询踪两个表的数据变化:

select * from test3
select * from test3_bak



郑重声明:资讯 【怎样用触发器对SQL Server进行数据备份(转载)_温故知新循序渐进_百度空间】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——