两个表A,B
B表中的y列的值都是从A表中x列里取的 我想让A.x的值变化时对应的B.y的那些值跟着变化。如何写?谢谢

解决方案 »

  1.   

    create trigger on a 
    for update 
    as 
    update b set y=inserted.x where y=deleted.x
      

  2.   

    create trigger tri_test on a
    for update
    as
    if update(x)
    update b set y=a.x from inserted a where a.id=b.id
      

  3.   

    create trigger Update_b on a
    for update ,insert ,delete
    as
    begin 
    if not exists(select 1 from deleted)--a表进行insert操作
    insert into b(y) select x from insertedif not exists(select 1 from inserted)--a表进行delete操作
    delete from b where y in (select x from deleted)if exists(select 1 from inserted) and exists(select 1 from deleted)--a表进行update操作
    update b set b.y=i.x from b,inserted i,deleted d
    where b.y=d.y
    end
      

  4.   

    to:chuifengde(树上的鸟儿)假如a表跟b表通过a表的x和b表的y想关联呢
      

  5.   

    A和B表唯一的联系就是B.y里的值全是A.x里面的 A.x和B.y是一对多  两个表的ID没联系 其他字段也没联系
      

  6.   

    if exists(select 1 from inserted) and exists(select 1 from deleted)--a表进行update操作
    update b set b.y=i.x from b,inserted i,deleted d
    where b.y=d.y
    end
    ~~~~~~~~~~~~~~~~~~~~~~
    d和i是哪儿来的呢?
      

  7.   

    我只需要A.x里的值改变的时候B.y里的跟着变 比如由1变成2 两个表里的相同的都是1的都变成2 不考虑删除和插入
      

  8.   

    把id改成对应的x和y不行?如果你是改主键和外键,那就在设计表中加上级连更新,它会自动改
      

  9.   

    回chuifengde(树上的鸟儿) 
    update b set y=a.x from inserted a where a.id=b.id
    ~~~~~~~~~~~~~~~~
    触发器里set y=a.x可以这样写?好像不认a.x吧~~
      

  10.   

    --两个表A,B
    --B表中的y列的值都是从A表中x列里取的 我想让A.x的值变化时对应的B.y的那些值跟着变化。如何写?谢谢
    create table a
    (
    uid int,
    uname varchar(50)
    )
    insert into a values(1,'taiji')
    insert into a values(2,'baguai')
    insert into a values(3,'tianlong')
    insert into a values(4,'cao')
    insert into a values(5,'ddd')
    insert into a values(6,'yun')
    insert into a values(7,'lijdjjie')
    select * from adrop table acreate table b
    (
    bid int identity,
    uname varchar(50)
    )insert into b(uname) select uname from acreate trigger DoOne
    on a for update 
    as
    update b set uname = (select uname from inserted ) where uname=(select uname from deleted) drop trigger DoOneupdate a set uname = 'aaa' where uid=1
      

  11.   

    create trigger DoOne
    on a for update 
    as
    update b set uname = (select uname from inserted ) where uname=(select uname from deleted) 
    这是重点
      

  12.   

    我刚才搞了下没反应阿~~能用我给的A.x B.y的形式帮写下么~谢谢
      

  13.   

    搞定了  谢谢tanghongmiao() 还有大家~~