请教个触发器的问题,一个表,两个字段,更新其中一个的时候,在触发器中根据这个字段去自动改另一个字段的值,如何写?
谢谢

解决方案 »

  1.   

    --计算列也可以
    create table tb(a int,b as a)
    goinsert into tb(a) select 1
    goupdate tb set a=2 
    goselect * from tbdrop table tb
      

  2.   

    create table a(id int, data int)
    insert a select 1,2
    union all select 3,5create trigger aa on a
    for update
    as
    begin
    update a set data=inserted.id from a,inserted where a.id=inserted.id
    end
      

  3.   

    create table tb(a int,b int)
    insert into tb select 1,1
    insert into tb select 2,2
    insert into tb select 4,4
    gocreate trigger cfq on tb
    for update
    asif @@rowcount=0 returnupdate tb set b=b.a from tb a,inserted b where a.a=b.a
    goupdate tb set a=2 where a=1
    update tb set a=3 where a=2
    goselect * from tbdrop trigger cfq
    drop table tb--触发器
      

  4.   

    CREATE TRIGGER [up] ON [dbo].[temptable] 
    FOR  UPDATE
    AS
    if update(name1)  
    update temptable set name2=Inserted.name1 from temptable,Inserted where temptable.id=Inserted.id