- 爱易网页
-
MSSQL教程
- 请问中!多谢.望好心人能帮忙
日期:2014-05-18 浏览次数:20449 次
请教中!谢谢.望好心人能帮忙
现有一表 "productstructuresEX "为产品结构表.
假如有两产品结构为:
一个A1需要两个B三个C,一个B需要两个D;
一个A2需要一个B两个C,一个B需要两个D
则在表里列示为:
autoid cpspcode cpscode ipsquantity
1 A1 B 2
2 A1 C 3
3 B D 2
4 A2 B 1
5 A2 C 2
现有一代码计算子件的需求数:
begin
declare @i int,@ret varchar(8000)
set @i = 1
select a.cpspcode,a.cpscode,a.ipsquantity as ipsquantity,@i as level into #t
from productstructuresEX
while @@rowcount <> 0
begin
set @i = @i + 1
print @i
insert into #t
select
a.cpspcode,a.cpscode,a.ipsquantity*B.ipsquantity,@i
from
productstructuresEX a,#t b
where
a.cpspcode=b.cpscode and b.Level = @i-1
end
delete t from #t t where exists(select 1 from #t where cpspcode=t.cpscode)
select cpscode,sum(ipsquantity) as 需求数 into #b from #t GROUP BY cpscode
end