表AID  CODE CODE1
1    3    0
2    3    0
3    4    0
4    4    0当update 表A set CODE1=2 WHERE ID=1 时,能否让所有CODE为3的行自动更新为2

解决方案 »

  1.   

    create trigger my_trig on a for update
    as
    begin
      update code1 = i.code1
      from a , inserted i 
      where a.code = i.code and i.code1 = 2 and i.id = 1
    endcreate trigger my_trig on a for update
    as
    begin
      update code1 = i.code1
      from a , inserted i 
      where a.code = i.code 
    end
      

  2.   

    --不能吧(个人认为),可在更新上述条件的同时写SQL更新CODE为3的行 :
    BEGIN TRAN
    update 表A set CODE1=2 WHERE ID=1
    update 表A set CODE=2 WHERE CODE=3
    COMMIT TRAN
      

  3.   

    create trigger tri_a on a 
    for update
    as
    begin
         if exists(select 1 from inserted where CODE1=2 and id=1) 
         begin
              update a
              set CODE=2
              where CODE=3
         end
    end
      

  4.   


    我的意思是当CODE相同时,只要更新其中一行,其它行自动更新,而且想在触发中实现,想
    问一下各位SQLSERVER是否能实现