自己编写,调用金碟K3的表!/****** 对象: UserDefinedFunction [dbo].[GetBOMTreeInfo] 脚本日期: 05/23/2009 17:22:27 编写:CMJ******/SET ANSI_NULLS OFFGOSET QUOTED_IDENTIFIER ONGO/****** 对象: 用户定义函数 dbo.GetBOMTreeInfo 2.0版本 脚本日期: 2007-09-19 20:18:43 ******/ALTER FUNCTION [dbo].[GetBOMTreeInfo] ( @ItemID as int) RETURNS @treeinfo table ( [fid] [int] IDENTITY (1, 1) NOT NULL , --真正的结点ID [id] [int] NOT NULL ,--结点内码[name] [varchar] (100) NOT NULL, --产品名称[parentid] [int] NOT NULL,--父结点内码 [itemid][int],--产品内码[Model][varchar](128),--规格型号[ERPClsID][int],--产品属性内码[DefaultRoutingID][int],--默认工艺路线内码[BomNum][varchar](300),--BOM单号[levels] [int] NOT NULL ,--BOM层次[fYield][decimal](18,2),--成品率[fScrap][decimal](18,2),--损耗率[fAuxQty][decimal](18,4),--用量[fNumber][varchar](255),--代码[fChartNumber][varchar](255),--图号[fUnitName][varchar](32),--产品单位[fRealStockNum][decimal](18,2),--实仓数量[fDummyStockNum][decimal](18,2),--虚仓数量--fReferNum[decimal](18,2),参照数量[fErpClsName][varchar](32),--产品属性名称[fUnitID][int],[fQty][decimal](18,10),--基本单位用量[fCZNum][decimal](24,10)--参照数) AS BEGIN declare @level as int,@BOMID as intselect @level=0select @bomid=finterid from icbom where fitemid=@ItemID and fusestatus=1072INSERT INTO @treeinfo SELECT a.finterid,b.fname,-1,a.fitemid,b.fmodel,b.ferpclsid,b.fdefaultroutingid,a.fbomnumber,@level,fYield,0,1,b.fnumber,b.fchartnumber, f.fname as fUnitName,g.fqty as fRealNumber,h.fqty as fDummyNumber,i.fname as ferpclsname,a.funitid,1,0FROM icbom a inner join t_icitem b on a.fitemid=b.fitemid left JOIN dbo.t_MeasureUnit f ON a.FUnitID = f.FMeasureUnitID left JOIN (SELECT fitemid, SUM(fqty) AS fqty FROM icinventory GROUP BY fitemid) g ON b.FItemID = g.fitemid LEFT JOIN (SELECT fitemid, SUM(fqty) AS fqty FROM poinventory GROUP BY fitemid) h ON b.FItemID = h.fitemid left join (select finterid,fname from t_submessage where ftypeid=210 and fparentid=0) i on b.ferpclsid=i.finteridwhere a.finterid=@BOMIDif @@rowcount=0 beginINSERT INTO @treeinfo SELECT @itemid,b.fname,-1,b.fitemid,b.fmodel,b.ferpclsid,b.fdefaultroutingid,'',@level,100,0,1,b.fnumber,b.fchartnumber, f.fname as fUnitName,g.fqty as fRealNumber,h.fqty as fDummyNumber,i.fname as ferpclsname,b.funitid,1,0FROM t_icitem b left JOIN dbo.t_MeasureUnit f ON b.FUnitID = f.FMeasureUnitID left JOIN (SELECT fitemid, SUM(fqty) AS fqty FROM icinventory GROUP BY fitemid) g ON b.FItemID = g.fitemid LEFT JOIN (SELECT fitemid, SUM(fqty) AS fqty FROM poinventory GROUP BY fitemid) h ON b.FItemID = h.fitemid left join (select finterid,fname from t_submessage where ftypeid=210 and fparentid=0) i on b.ferpclsid=i.finteridwhere b.fitemid=@itemid and b.ferpclsid<>2 goto MetEndendWhile 1=1begin insert into @treeinfo select a.fentryid,b.fname,z.fid,a.fitemid,b.fmodel,b.ferpclsid,b.fdefaultroutingid,Null,@level+1,100,a.fScrap,a.fAuxQty,b.fnumber,b.fchartnumber, f.fname as fUnitName,g.fqty as fRealNumber,h.fqty as fDummyNumber,i.fname as ferpclsname,a.funitid,a.fQty,0 from (select * from @treeinfo where levels=@level) z left join icbomchild a on a.finterid=z.id inner join t_icitem b on a.fitemid=b.fitemid and b.ferpclsid<>2 left JOIN dbo.t_MeasureUnit f ON a.FUnitID = f.FMeasureUnitID left JOIN (SELECT fitemid, SUM(fqty) AS fqty FROM icinventory GROUP BY fitemid) g ON b.FItemID = g.fitemid LEFT JOIN (SELECT fitemid, SUM(fqty) AS fqty FROM poinventory GROUP BY fitemid) h ON b.FItemID = h.fitemid left join (select finterid,fname from t_submessage where ftypeid=210 and fparentid=0) i on b.ferpclsid=i.finterid insert into @treeinfo select a.finterid,b.fname,c.fid,a.fitemid,b.fmodel,b.ferpclsid,b.fdefaultroutingid,a.fbomnumber,@level+1,fYield,fScrap,1,b.fnumber,b.fchartnumber, f.fname as fUnitName,g.fqty as fRealNumber,h.fqty as fDummyNumber,i.fname as ferpclsname,a.funitid,1,0 from icbom a inner join t_icitem b on a.fitemid=b.fitemid and b.ferpclsid=2 inner join (select a.finterid,a.fitemid,a.fScrap,b.fid from icbomchild a inner join @treeinfo b on a.finterid=b.id and b.levels=@level) c on a.fitemid=c.fitemid left JOIN dbo.t_MeasureUnit f ON a.FUnitID = f.FMeasureUnitID left JOIN (SELECT fitemid, SUM(fqty) AS fqty FROM icinventory GROUP BY fitemid) g ON b.FItemID = g.fitemid LEFT JOIN (SELECT fitemid, SUM(fqty) AS fqty FROM poinventory GROUP BY fitemid) h ON b.FItemID = h.fitemid left join (select finterid,fname from t_submessage where ftypeid=210 and fparentid=0) i on b.ferpclsid=i.finterid where a.fusestatus=1072 if @@rowcount=0 break else select @level=@level+1 endupdate a set a.fauxqty=b.fauxqty from @treeinfo a inner join icbomchild b on a.itemid=b.fitemid and a.erpclsid=2 and a.levels<>0MetEnd:RETURN END