例如:我现在有两个表,表A和表BA表
id    c列    d列
1     aa     ww
2     qq     ee
B表
id    A_id  c列
4       1    aa
5       2    qq现在我想更新表A的C列中 id = 1的aa = bb,在更新A表的同时,B表A_id = 1 的C列也同时更新为 aa = bb

解决方案 »

  1.   

    create trigger 觸發器 on A
    after update
    as
    begin  update B
            set colc=bb
       where A_id in (select id from deleted)  
    end
      

  2.   

    update A set c='bb' where id=1
    update B set c='bb' where A_id=1
      

  3.   

    create trigger tri on A
    for update
    as
    begin  update B
            set colc='bb'
        where A_id in (select id from deleted)  
    end
      

  4.   

    直接更新表:
    update a set c列='bb' where id=1
    update b set c列='bb' where a_id=1用触发器:
    create trigger tria on a 
    for update
    as
       if update(c列)
          update b set c列=aa.c列 from inserted aa where aa.id=a_id
      

  5.   

    --orcreate trigger 觸發器 on A
    after update
    as
    begin  update B
            set colc='bb'
      from B a ,deleted b
      where a.id=b.A_id  
    end
      

  6.   

    --建立测试环境
    set nocount on
    create table b(id varchar(20),A_id varchar(20),c列 varchar(20))
    insert into b select '4','1','aa'
    insert into b select '5','2','qq'
     create table a(id varchar(20),c列 varchar(20),d列 varchar(20))
    insert into a select '1','aa','ww'
    insert into a select '2','qq','ee'
    go
    --测试
    create trigger test on b 
    for update
    as
    begin
    update a set c列=i.c列 
    from inserted i inner join a on A_id=a.id
    end
    go
    update b set c列='bb' where A_id='1'
    select * from a
    --删除测试环境
    drop table b
    drop table a
    set nocount off/*
    1 bb ww
    2 qq ee
    */
      

  7.   

    create trigger A_update on a
    after update
    as
    upate b
    set c列=i.c列
    from inserted i join b on i.ID=b.A_id