发现一个没有考虑周全的问题,亟待解决!
商品表goods primary key是Gcode
Gcode Gname Gnorm Gprice
1001 a1 red 12
1002 b4 green 30
1003 z7 new .7
1004 e6 yellow 5
销售总表Ssum primary key是 Scode
Scode CusName Sdate Scost
06300001 cos1 <Binary> 50 这里不应该是50,应该是表Sbill中所有Scode=06300001 的总值!!!!!!!
06300002 cos2 <Binary> 24.5
06300003 cos3 <Binary> -
06300004 cos4 <Binary> -
销售明细表Sbill 其中code 是 KEY FK并Scode ref Ssum.Scode Gcode 是 KEY FK并Gcode ref Goods.Gcode
Scode Gcode Samount price
06300001 1002 10 30
06300001 1004 10 5
06300002 1003 5 .7
其中表Ssum的值是根据表Sbill相应信息计算得到的,
由于表Sbill有2个KEY FK 所以Scode 与 Gcode 不同时相同就可以。例如表Sbill的头两行记录
做了一个trigger,就是要完成修改或插入表Sbill的Samount,对表Ssum的Scost都有影响,
但是发现计算的结果,怎么只是表Sbill最后一个Scode 相等时的数计算出的结果?
请问怎么才能得到想要的“总值”?
原来的得trigger是:
use pubs;
go
if exists(select name from sysobjects
where name='ReCalculateCost' and type='TR')
drop trigger ReCalculateCost
go
create trigger ReCalculateCost
on Sbill
for update,insert,delete
as
begin
update Ssum
set Ssum.Scost=Sbill.Samount*Sbill.price
from Sbill,Ssum
where Ssum.Scode=Sbill.Scode //是这里的条件不对吗?
end