实现这样的效果A 表的 添加、更新、删除操作都在B表中做日志A表结构
A_ID = 自动增列(主键)
A_Info = 记录内容B表结构
OptionPKID = 操作的A表的对应主键ID
OptionType = 1添加操作 /  2更新操作  / 3删除操作

解决方案 »

  1.   

    --在b中加了个日期字段,记录操作时间
    create table a(A_ID int identity(1,1) primary key,A_Info varchar(50))
    gocreate table b(OptionPKID int,OptionType varchar(10),[date] datetime)
    gocreate trigger cfq on a
    for insert,update,delete
    asif @@rowcount=0 returndeclare @count_del int
    declare @count_ins int
    set @count_del=0
    set @count_ins=0select @count_del=count(*) from deleted
    select @count_ins=count(*) from insertedif (@count_ins>0 and @count_del=0)
    begin
      if @@error=0
        insert into b select A_ID,'插入操作',getdate() from inserted
    endif (@count_ins>0 and @count_del>0)
    begin
      if @@error=0
        insert into b select A_ID,'更新操作',getdate() from inserted
    endif (@count_ins=0 and @count_del>0)
    begin
      if @@error=0
        insert into b select A_ID,'删除操作',getdate() from deleted
    end
    go
    insert into a select 'song'
    insert into a select 'rr'
    insert into a select 'sxx'
    insert into a select 'uu'
    select * from a
    select * from b
    update a set A_info='111' where A_ID=1
    select * from a
    select * from b
    delete a where A_info='111'
    delete a where A_info='rr'
    select * from a
    select * from b
    drop trigger cfq
    drop table a
    drop table b