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

前端之家收集整理的这篇文章主要介绍了SQLServer 触发器 数据库进行数据备份前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<div class="codetitle">@L_403_0@ 代码如下:

<div class="codebody" id="code30295">
  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,bid int,
  uage int,active char(1));

 

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


<div class="codetitle"><a style="CURSOR: pointer" data="22010" class="copybut" id="copybut22010" onclick="doCopy('code22010')"> 代码如下:
<div class="codebody" id="code22010">
  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

  第三步,测试数据:

<div class="codetitle"><a style="CURSOR: pointer" data="99983" class="copybut" id="copybut99983" onclick="doCopy('code99983')"> 代码如下:
<div class="codebody" id="code99983">
  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

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


<div class="codetitle"><a style="CURSOR: pointer" data="39504" class="copybut" id="copybut39504" onclick="doCopy('code39504')"> 代码如下:
<div class="codebody" id="code39504">
  select from test3
  select
from test3_bak

原文链接:https://www.f2er.com/mssql/64329.html
数据备份触发器触发器触发器

猜你在找的MsSQL相关文章