CREATE TRIGGER [update_compute] ON [dbo].[t]
FOR INSERT, UPDATE
AS
begin
if upadte(b)
set set t.c = t.a + t.b
if update(a)
set t.c = t.a + t.b
end
FOR INSERT, UPDATE
AS
begin
if upadte(b)
set set t.c = t.a + t.b
if update(a)
set t.c = t.a + t.b
end
FOR INSERT, UPDATE
AS
begin
update t
set c = b.a + b.b from t a,inserted b where a.c=b.c
end
试一下
FOR INSERT, UPDATE
AS
begin
if update(a) or update(b)
set t.c = t.a + t.b
end
create table t_test (a int,b int,c int)假如字段a为主键:create trigger tr_test on t_test
for insert,update
as
begin
update t_a set t_a.c=isnull(t_b.b,0)+isnull(t_b.a,0) from t_test as t_a,inserted as t_b where t_a.a=t_b.a
end
for insert,update
as
begin
update t_a set t_a.c=isnull(t_b.b,0)+isnull(t_b.a,0) from t_test as t_a,inserted as t_b where t_a.主键=t_b.主键
end
有更好的方法吗?
insert into t values(x,y,x+y)
这不就行了吗?