两张表A和B,字段全部相同
A原表 保存原始的数据
B副表 保存修改后的数据
例如
A表内容
hwmc js tj zl
鞋子 10 3 4B表内容 修改后为
hwmc js tj zl
衣服 10 3 4要能把hwmc,js,tj,zl 这几个字段中的内容变化 找出来 ,
例如上例:hwmc 由鞋子改为了衣服要能输出  货物名称由鞋子--->衣服

解决方案 »

  1.   

    这个要求如果用CDC的话挺好的,因为它可以记录修改前和修改后、什么时候修改。这样追踪问题挺好,触发器的话还会对性能有点影响。
      

  2.   

    create table subject
    (
     col1 int
    )
    create table t_record
    (
      action_id    numeric(10) identity(1,1),
      action_time  datetime,
      action       varchar(10)
    )
    --触发器
    create trigger trig_subject on subject
    for insert,update,delete
    as
    begin
      declare @inserted int
      declare @deleted int
      select @inserted = count(*) from inserted
      select @deleted = count(*) from deleted
      
      if @inserted > 0 and @deleted > 0
      begin
        insert into t_record values(getdate(),'update')
      end
      else if @inserted > 0 and @deleted = 0
      begin
        insert into t_record values(getdate(),'insert')
      end
      else if @inserted = 0 and @deleted > 0
      begin
        insert into t_record values(getdate(),'delete')
      end
    end
    --测试
    insert into subject values(1)
    --结果如下/*
    1 2009-06-04 16:36:03.530 insert
    */
    --结果如下
    update subject set col1 = 2 where col1=1
    1 2009-06-04 16:36:03.530 insert
    2 2009-06-04 16:36:22.390 update--结果如下
    /*
    delete subject where col1 = 2
    1 2009-06-04 16:36:03.530 insert
    2 2009-06-04 16:36:22.390 update
    3 2009-06-04 16:36:40.080 delete 
    */给你一个很简单的例子,触发器监控表的示例