写了个简易的MRP根据传进来的数据,进行上下级的判断,父表,子表
构思:
3张变量表,第一张用来存传进来的成品,这边对表做了循环就是成品是一样一样进去的,取成品表的第一行,将数据做父表和子表关联,取出有关的物料,存到第二张变量表。
再次做一个循环,将第二张变量表和父表做关联,如果有数据存在将数据存到第三张变量表,根据第三张变量表删掉第二张变量表中相同的数据,然后对第三张变量表和父表子表做关联,将数据反填回第二张变量表,并清空第三张变量表,循环到没有条件成立,即可。
下面是我的方法:
----------------begin----------------------------
declare @wltemp table(id int,code varchar(50))
insert into @wltemp(id,code)
select '1','PAR-471J0-60'
--union
--select '2','PAR-102J0-60'
declare @temp table(fid int,mainid int,num int,whscode int)
declare @temp1 table(fid int,mainid int,num int,whscode int)
declare @i int
declare @maxvalue int
select @maxvalue=count(1) from @wltemp
select @i=1
while(@i<=@maxvalue)
begin
if exists(select top 1 1 from dchild1727 a inner join dfather1727 b on a.fid=b.id
inner join ditm c on b.mainid=c.id inner join @wltemp d on c.fathercode=d.code where d.id=@i)
begin
insert into @temp(fid,mainid,num,whscode)
select a.fid,a.mainid,a.c_num,a.whscode from dchild1727 a inner join dfather1727 b on a.fid=b.id
inner join ditm c on b.mainid=c.id inner join @wltemp d on c.fathercode=d.code where d.id=@i
end
set @i=@i+1
-------------------------上部分为调取成品名字--------------------下部分为循环-------------------------------
while exists(select top 1 1 from dfather1727 a inner join @temp b on a.mainid=b.mainid)
begin
insert into @temp1(fid,mainid,num,whscode)
select b.fid,b.mainid,b.num,b.whscode from dfather1727 a inner join @temp b on a.mainid=b.mainid
delete from @temp where mainid in(select mainid from @temp1 a where a.mainid=mainid and a.fid=fid)
insert into @temp(fid,mainid,num,whscode)
select a.fid,a.mainid,a.c_num,a.whscode
from dchild1727 a inner join dfather1727 b on a.fid=b.id inner join @temp1 c on b.mainid=c.mainid
delete from @temp1
end
end
SELECT b.fathercode 物料编码,b.itemname 物料名称,a.whscode 仓库,a.num 数量 from @temp a inner join ditm b on a.mainid=b.id
---------------end-------------