表table如下姓名 高数 计算机 英语 总分
张三  80   90    56 
李四  90   56     25请问怎么编写触发器,自动计算和啊??谢谢,新人,没有人

解决方案 »

  1.   

    本帖最后由 libin_ftsafe 于 2007-10-25 17:41:37 编辑
      

  2.   

    create table test(ID int identity(1,1),Name varchar(10),Score1 int,Score2 int,Score3 int,Total int)
    go
    create trigger Up_Total on test
    after insert,update
    as
    update t set Total=isnull(t.Score1,0)+isnull(t.Score2,0)+isnull(t.Score3,0)
    from test t join inserted i on t.ID=i.ID
    go
    insert test(Name,Score1,Score2,Score3) select '张三',  80,   90,    56  
    insert test(Name,Score1,Score2,Score3) select '李四',  90,   56,    25 go
    select * from test--drop table test
    ID          Name       Score1      Score2      Score3      Total       
    ----------- ---------- ----------- ----------- ----------- ----------- 
    1           张三         80          90          56          226
    2           李四         90          56          25          171(所影响的行数为 2 行)