当前位置: 首页 > 图文教程 > 数据库 > MSSQL > 触发器对SQL Server数据库进行备份

MSSQL
用SQL Server为Web浏览器提供图像(三)(转)
用SQL Server为Web浏览器提供图像(四)(转)
数据库连接范例
数据访问-与数据库建立连接
SQL Server静态页面导出技术1
SQL Server静态页面导出技术2
Access数据库安全的几个问题
保护Access 2000数据库的安全
ACCESS97关于数据库安全的几个问题
如何把access转换到mysql
将Access数据转换为XML格式
通过Access从Web获取MySQL数据
如何将Access和Excel导入到Mysql中之一
如何将Access和Excel导入到Mysql中之二
如何将Access和Excel导入到Mysql中之三
ACCESS数据库向MySQL快速迁移小程序(一)
ACCESS数据库向MySQL快速迁移小程序(二)
长期使用中型Access数据库的一点经验
微软SQL Server 2005的30项顶尖特性
SQL Server2000企业版安装教程

MSSQL 中的 触发器对SQL Server数据库进行备份


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-30   浏览: 56 ::
收藏到网摘: n/a

问:如何用触发器对SQL Server数据库进行数据备份?

答:首先,你需要建立测试数据表,一个用于插入数据: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));

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

以下是引用片段:

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 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

最后,你可自己采用下面的查询踪两个表的数据变化:

以下是引用片段:

select * from test3
select * from test3_bak