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 行)
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 行)