--可以但不限于使用触发器
--不知道你所谓的“写入”是指插入还是更新
create trigger tri_tb1 on tb1
for update
as
if update(A)
update tb2
set B=d.A
from deleted d
where tb2.id=d.id
go

解决方案 »

  1.   

    谢谢szx1999的回复,还有些不明白1、除了用触发器,还可以用什么简单办法实现?2、表2是用于记录的,所以是写入,不是更新,这块该如何写?
      

  2.   

    有个工具,OMNIAUDIT,你可以搜索下。这个是动态审计数据的工具,可以实现你的目的
      

  3.   

    你把update语句换成insert不就行了。。
    create trigger tri_tb1 on tb1
    for update
    as
        if update(A)
            insert tb2(B) select A from deleted
    go
      

  4.   


    1:你可以在修改语句之前加一句
    insert into tb2(B) 
    select A from tb1 where 条件 A<>'A'(更改的A)
      

  5.   

    create trigger tri_tb1 on tb1
    for update
    as
    if exists(select 1 from tb1 a join inserted b on a.id = b.id and a.字段A <> b.字段A)
    insert tb2 select * from inserted 
      

  6.   

    create table tb1(cola int)
    insert into tb1 select 2 union all select 3
    create table tb2(colb int)
    go
    create trigger tg1
    on tb1
    after update
    as
    insert into tb2(colb) select cola from deleted a where not exists(select 1 from inserted where cola=a.cola)
    go
    update tb1 set cola=5 where cola=2
    update tb1 set cola=3 where cola=3
    go
    select * from tb1
    select * from tb2
    go
    drop table tb1,tb2
    /*
    cola
    -----------
    5
    3(2 行受影响)colb
    -----------
    2(1 行受影响)*/
      

  7.   

    谢谢大家,已经解决问题,最后的办法是用的szx1999的办法:create trigger tri_tb1 on tb1
    for update
    as
        if update(A)
            insert tb2(B) select A from inserted
    godeleted表 存的就是表1更新之前的内容, inserted表 存的才是更新之后的内容。