For example:create table test(a int,b int,c as a*b) goinsert into test(a,b) values(5,6) select * from test go/* a b c ----------- ----------- ----------- 5 6 30 */drop table test go
create trigger tri_insert on t after update,insert as update t set c=t1.a*t1.b from inserted t1 where t.id=t1.id
create table t ( id int identity(1,1), a int, b int, c int )create trigger aa on t after insert ,update as update t set c=t1.a*t1.b from inserted t1 where t.id=t1.idinsert into t select 12,10 ,null union all select 5,6,nullselect * from tid a b c ----------- ----------- ----------- ----------- 1 12 10 120 2 5 6 30(2 row(s) affected)
goinsert into test(a,b) values(5,6)
select * from test
go/*
a b c
----------- ----------- -----------
5 6 30
*/drop table test
go
然后在表达式上写 a*bc根本就是多余的
要用的时候可直接 select a,b,a*b as c from tab
现在使用的是一套商品化软件,表结构都是定死的,原来a\b\c列之间没有任何关系,我想设定这样一种关系:c=a*b,输入a\b后自动计算c,而且是在该记录保存之前计算。
after update,insert
as
update t
set c=t1.a*t1.b from inserted t1
where t.id=t1.id
(
id int identity(1,1),
a int,
b int,
c int
)create trigger aa on t
after insert ,update
as
update t
set c=t1.a*t1.b
from inserted t1
where t.id=t1.idinsert into t
select 12,10 ,null union all
select 5,6,nullselect * from tid a b c
----------- ----------- ----------- -----------
1 12 10 120
2 5 6 30(2 row(s) affected)