if(exists(select 1 from 表1 where 列1=b.列1)
begin
select 列1,a.列2,列3=b.列2*a.列5
from 表2 b,表1 a
where a.列1=b.列1
union all
select 列1,a.列2,列3=b.列3*a.列6
from 表2 b,表1 a
where a.列1=b.列1
...
end
begin
select 列1,a.列2,列3=b.列2*a.列5
from 表2 b,表1 a
where a.列1=b.列1
union all
select 列1,a.列2,列3=b.列3*a.列6
from 表2 b,表1 a
where a.列1=b.列1
...
end
表A:
列1 列2 列3 列4 列5 列6 列7 .......
A 1 2 3 4 5 6 .......表B
列1 列2 列3 列4 列5 列6 列7表C
列1 列2 列3 当表B输入数据,并表A列1的数据等于表B列1的数据时,即 :
列1 列2 列3 列4 列5 列6 列7
A 8 9 10 11 12 13
表B中的列2数据乘以表A的列5到列20的数,(表A中的 列6以后数据不一定有,可能为空。)
把运算的结果写入表C:
结果如下:
列1 列2 列3
A 1 32
A 1 40
A 1 48
A 1 56
.. .. ..
on B
for insert
as
begin insert C
select *
from ( select a.列1,a.列2,b.列2*a.列5
from a join inserted b on a.列1=b.列1
where a.列5 is not null
union all
select a.列1,a.列2,b.列2*a.列6
from a join inserted b on a.列1=b.列1
where a.列6 is not null
union all
......
union all
select a.列1,a.列2,b.列2*a.列20
from a join inserted b on a.列1=b.列1
where a.列20 is not null
) dend
go