金蝶K3 Wise版本BOM批量多级展开代码

K3没有批量BOM多级展开功能,如果需要对所有BOM进行批量多级展开,可以使用如下sql代码来实现。

 

1、创建BOM父项表

 
  1. create table HWbom

  2. (

  3. FId int identity(1,1),

  4. FItemID int

  5. )

2、创建BOM子项表

 

 
  1. create table HWbomchild

  2. (

  3. FID int identity(1,1),

  4. FOrgID int,

  5. FParentID int,

  6. FLevel int,

  7. FSN nvarchar(200),

  8. FItemID int,

  9. FQty decimal(28,19),

  10. FBOMInterID int,

  11. FEntryID int

  12. )


3、将父项BOM对应的物料内码插入父项表(可带条件)

 

 
  1. insert into HWbom

  2. (FItemID)

  3. select t.Fitemid from t_ICItem t

  4. inner join t_item t5 ON t5.FItemID = t.Fitemid

  5. left join icbom t6 on t6.fitemid=t.Fitemid

  6. left join ICBOMGROUP t7 on t7.finterid=t6.fparentid

  7. where

  8. t.FErpClsID in (2,3,5)--2代表自制件,3代表委外件,5代表虚拟件

  9. and t5.fdeleted=0

  10. and t5.fnumber in('物料编码1','物料编码2')--可根据需要限定BOM范围

  11. order by t.fnumber

4、根据父项表数据,将数据插入子项表,为后续卷算做准备

 

 

 
  1. insert into HWbomchild

  2. (FOrgID,FParentID,FSN,FItemID,FQty,FBOMInterID,FEntryID,FLevel)

  3. select

  4. FId,-1 as FParentID,'001',u1.FItemID,1 as Fqty,t1.FInterID as FBOMInterID,-1 as FEntryID,0

  5. from HWbom u1

  6. left join ICBOM t1 on u1.FItemID=t1.FItemID

5、BOM卷算

 

 

 
  1. declare @level int

  2. set @level=1

  3.  
  4. while exists(

  5. select 1 from

  6. HWbomchild

  7. where FLevel=@level-1

  8. and FItemID in

  9. (select Fitemid from icbom)

  10. )

  11. and @level<20

  12. begin

  13.  
  14. insert into HWbomchild

  15. (FOrgID,FParentID,FSN,FItemID,FQty,FBOMInterID,FEntryID,FLevel)

  16. select

  17. u1.FOrgID,u1.FID,u1.FSN+'.'+right('000'+CONVERT(nvarchar(50),t2.Fentryid),3),t2.Fitemid,u1.FQty*(t2.FQty/t1.FQty)/(1-t2.FScrap/100),

  18. t2.FInterID,t2.FEntryID,@level

  19. from HWbomchild u1

  20. inner join icbom t1 on u1.FItemID=t1.FItemID

  21. inner join ICBOMChild t2 on t2.FInterID=t1.FInterID

  22. where u1.FLevel=@level-1

  23.  
  24. set @level=@level+1

  25.  
  26. end

6、最终BOM展开数据检索,执行结果可复制到Excel 

 
  1. select

  2. t2.FNumber 产品代码,t2.FName 产品名称,t2.FModel 产品规格,

  3. t1.FSN 序号,

  4. t3.FNumber 材料代码,t3.FName 材料名称,t3.FModel 材料规格,t1.FQty 产品用量,yy.fname 材料属性,

  5. t5.FQty 单位用量,

  6. t5.FScrap 损耗率,

  7. t4.FBOMNumber BOM编号,

  8. t6.FName as 是否跳层,

  9. t5.FNote 备注,

  10. t5.FPositionNo 位置号

  11. from HWbom u1

  12. inner join HWbomchild t1 on u1.FId=t1.FOrgID

  13. inner join t_icitem t2 on t2.FItemID=u1.FItemID

  14. inner join t_ICItem t3 on t3.FItemID=t1.FItemID

  15. left join ICBOM t4 on t4.FInterID=t1.FBOMInterID

  16. left join ICBOMChild t5 on t5.FInterID=t1.FBOMInterID and t5.FEntryID=t1.FEntryID

  17. left join t_SubMessage t6 on t6.FInterID=t4.FBOMSkip

  18. inner join t_SubMessage yy on yy.FInterID=t3.FErpClsID

  19. --where fbomnumber='物料编码'

  20. order by u1.FId,t1.FSN


7、最后清空父项表和子项表数据,以便下次运算使用(不用重新建表了)

 

 

 
  1. TRUNCATE TABLE HWbom

  2. --清空附表数据

  3. TRUNCATE TABLE HWbomchild

  4. --清空子表数据


8、如果不再使用,可以将这两个表删除

 

 

 
  1. drop table HWbom

  2. --删除父表

  3. drop table HWbomchild

  4. --删除子表

上一篇:leetcode 264 Ugly Number II


下一篇:每日一题