新闻动态
新闻动态
NEWS INFORMATION

SQLServer 触发器 数据库进行数据备份

发布日期:2022-02-01 08:33 | 文章来源:源码中国
复制代码 代码如下:

  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

美国服务器租用

版权声明:本站文章来源标注为YINGSOO的内容版权均为本站所有,欢迎引用、转载,请保持原文完整并注明来源及原文链接。禁止复制或仿造本网站,禁止在非www.yingsoo.com所属的服务器上建立镜像,否则将依法追究法律责任。本站部分内容来源于网友推荐、互联网收集整理而来,仅供学习参考,不代表本站立场,如有内容涉嫌侵权,请联系alex-e#qq.com处理。

实时开通

自选配置、实时开通

免备案

全球线路精选!

全天候客户服务

7x24全年不间断在线

专属顾问服务

1对1客户咨询顾问

在线
客服

在线客服:7*24小时在线

客服
热线

400-630-3752
7*24小时客服服务热线

关注
微信

关注官方微信
顶部