直接设置字段f的公式为 a+b

解决方案 »

  1.   

    不是,我主要想怎么写这个触发器 其实f设置什么值是根据我条件的,不一定是a+b
      

  2.   

    create trigger tr_insert_update on table1
    for insert,update
    as
    update a set f=a+b
    from table1 a
    where exists(select * from inserted where 主键=a.主键)
      

  3.   


    zjcxc邹建的意思是不是在建表时可以——》CREATE TABLE mytable 
       (
        a int,
        b int,
        f AS (a+ b)
       )
      

  4.   

    真不好意思,刚才描述有问题
    我主要的是想怎么写当插入新记录时,该条记录f的值根据一定条件改变,
    当修改a或者b是 f的值也根据一定条件改变
      

  5.   

    create trigger tr_insert_update on table1
    for insert,update
    as
    update a set f=a+b
    from table1 a
    where exists(select * from inserted where 条件)
      

  6.   

    if exists (select * from sysobjects where name='test') drop table testcreate table test (a int,b int,c int,d int,e int,f int)--建触发器
    if exists (select * from sysobjects where name='tg_1' and type='TR') drop trigger tg_1
    go
    create trigger tg_1 on test
    for insert as
    update test set f=test.a+test.b from inserted
    go
    --测试
    insert into test (a,b,c,d,e)
    values(1,2,3,4,5)
    --删除
    select * from test
    drop table test
    ----------------------------------------(所影响的行数为 1 行)
    (所影响的行数为 1 行)