SQL codeset ANSI_NULLS ONset QUOTED_IDENTIFIER ONgoALTER PROCEDURE [dbo].[zzz_cz_wyh_bom]@Fnumber nvarchar(255) ASdeclare @finterid intdeclare @fitemid intselect @fitemid=fitemid from t_icitemwhere --fnumber='01.HY.HY-1/116-B-S'[email protected]select @finterid=finteridfrom icbomwhere [email protected] -- and fusestatus=1072Create Table #Errorss ( FIndex int IDENTITY, FType smallint default(0),FBomNumber int default(0), FErrText nvarchar(355) )Create Table #data1 ( FIndex int IDENTITY, FBomInterid int, FItemID int null, FNeedQty decimal(28,8) default(0) null, FBOMLevel int null, FItemType int null, FParentID int default(0)null, FRate decimal(20,8) default(0) null, FHistory int default(0) null, FHaveMrp smallint default(0) null, FLevelString nvarchar(200) null, FBom int ) exec PlanBomNestingCheck @finteridCreate Table #Mutidata ( FIndex int IDENTITY, FEntryID INT, FBomInterid int, FItemID int null, FNeedQty decimal(28,14) default(0) null, FBOMLevel int null, FItemType int null, FParentID int default(0)null, FRate decimal(28,14) default(0) null, FHistory int default(0) null,FHaveMrp smallint default(0) null, FLevelString varchar(200) null, FBom int, FMaterielType int default(371) null,FOperID int default(0),FCZQualityCheck bit) Create Table #MutiParentItem(FIndex int IDENTITY,FEntryID INT default(0), FBomInterid int, FItemID int null, FNeedQty decimal(28,14) default(0) null, FBOMLevel int null, FItemType int null, FParentID int default(0)null, FRate decimal(28,14) default(0) null, FHistory int default(0) null, FHaveMrp smallint default(0) null, FLevelString varchar(200) null , FBom int, FMaterielType int default(371) null,FOperID int default(0),froutingid int null,FCZQualityCheck bit) Create Table #Errors ( FIndex int IDENTITY, FType smallint default(0), FErrText varchar(355) )Insert into #mutiParentItem (fbominterid,FItemID,FNeedQty,FBOMLevel,FParentID,FItemType,FBom,froutingid,FCZQualityCheck) Select a.finterid, t1.FItemID,a.fqty, 0,0,t1.FErpClsID,t1.FItemID, a.FRoutingid,t1.FCZQualityCheck From icbom a,t_ICItem t1 Where t1.FItemID = a.fitemid and [email protected] declare @P1 intset @P1=0declare @P2 char(400)set @P2=' 'exec PlanMutiBomExpandEx 50, 1, '1900-01-01', '2100-01-01', @P1 output, @P2 outputCreate Table #Mutidata1 ( FIndex int IDENTITY, FEntryID INT, FBomInterid int, FItemID int null, FNeedQty decimal(28,14) default(0) null, FBOMLevel int null, FItemType int null, FParentID int default(0)null, fprice decimal(28,14) default(0) null, famount decimal(28,14) default(0) null, FLevelString varchar(200) null, FBom int, FMaterielType int default(371) null,FOperID int default(0),FCZQualityCheck bit) insert into #Mutidata1(fentryid,fbominterid,fitemid,fneedqty,fbomlevel,fitemtype,fparentid,flevelstring,fbom,fmaterieltype,foperid,FCZQualityCheck)select fentryid,fbominterid,fitemid,fneedqty,fbomlevel,fitemtype,fparentid,flevelstring,fbom,fmaterieltype,foperid,FCZQualityCheckfrom #Mutidataorder by findexupdate t1 set t1.fprice=(select fprice from icpurchaseentry t2 where t2.fdetailid= ( select max(t3.fdetailid) from icpurchaseentry t3 inner join t_icitem t4 on t3.fitemid=t4.fitemid and t4.fitemid=t1.fitemid) ) from #Mutidata1 t1 where fitemtype=1 update t1 set famount=isnull(isnull(fprice,0)*isnull(fneedqty,0),0)from #Mutidata1 t1update t1 set famount=(select sum(famount) from #Mutidata1 )from #Mutidata1 t1where fbomlevel=0Create Table #test( FIndex int , FItemID int null,fitemtype int null, FBOMLevel int null, flevel varchar(255)null,Fnumber varchar(255) null, Fname varchar(255) null, fsm varchar(255) null,fmodel varchar(255) null, fdraw varchar(255) null, Fmaterial varchar(200) null, Fdrawsize varchar(200) null,funit varchar(255) null,frouting varchar(255) null,Fqty decimal(28,14) default(0) null, fattid int null,fattname varchar(255) null,fbomnumber varchar(255) null,fremark varchar(255) null,FCZQualityCheck bit) insert into #testselect a.FIndex,a.FitemID,a.FItemType, a.FBOMLevel ,a.FLevelString as '层次(LEVEL)' ,b.fnumber as '物料代码(Number)',b.fname as '物料名称(Name)','',isnull(b.FModel,'') '规格(Spec.)',isnull(b.fchartnumber,'') '图号(Draw No.)',isnull(b.f_669,'') '材质(Material)',isnull(b.f_668,'') '图幅(Size of Drawing)', isnull(c.Fname,'') '单位(Unit)', tr.Froutingname as '工艺路线(Routing)', a.FNeedQty '用量(Qty)',--a.famount '材料成本(Cost)',b.FErpClsID, isnull(e.Fname,'') '物料属性(Mtr.Att.)',a.FCZQualityCheck, '' 'BOM编号(BOM NUM.)',d.FNote as '备注(Remark)',a.FCZQualityCheck-- ,-- isnull(i.fname,'') '状态(Status)' from #Mutidata1 a inner join t_icitem b on a.fitemid=b.fitemid left outer join t_item c on b.funitid=c.fitemid inner join icbomchild d on a.FBomInterid=d.finterid and a.FOperID=d.FOperID AND a.FEntryID=d.FEntryID and a.FItemID=d.FItemID left outer join t_submessage e on b.FErpClsID=e.finterid left outer join t_submessage f on d.FOperID=f.finterid left outer join t_stock g on d.FStockID=g.FItemID inner join t_item h on b.fitemid=h.fitemid left outer join t_routing Tr on b.fitemid =tr.fitemid left outer join t_submessage i on b.fusestate=i.finterid where 1=1 union select a.FIndex,a.FitemID,a.FItemType, a.FBOMLevel,a.FLevelString FLevel,b.fnumber FNumber,b.fname FName,'',isnull(b.FModel,'') FModel,isnull(b.fchartnumber,'') fchartnumber, isnull(b.f_669,''),isnull(f_668,''),isnull(c.Fname,'') FUnitID,tr.Froutingname, a.FNeedQty FQty,--a.famount, b.FErpClsID,isnull(e.Fname,'') FMaterielType, '' FBomNumber,d.FNote-- ,-- isnull(i.fname,'') FUseStatus from #Mutidata1 a inner join t_icitem b on a.fitemid=b.fitemid left outer join t_item c on b.funitid=c.fitemid inner join iccustbomchild d on a.FBomInterid=d.finterid and a.FItemID=d.FItemID and a.FOperID=d.FOperID AND a.FEntryID=d.FEntryID left outer join t_submessage e on b.FErpClsID=e.finterid left outer join t_submessage f on d.FOperID=f.finterid left outer join t_stock g on d.FStockID=g.FItemID inner join t_item h on b.fitemid=h.fitemid left outer join t_routing Tr on b.fitemid =tr.fitemid left outer join t_submessage i on b.fusestate=i.finterid where 1=1 -- union select a.FIndex,a.FitemID,a.FItemType, a.FBOMLevel,a.FLevelString FLevel,b.fnumber FNumber,b.fname FName,'',isnull(b.FModel,'') FModel,isnull(b.fchartnumber,'') fchartnumber, isnull(b.f_669,''),isnull(b.f_668,''),isnull(c.Fname,'') FUnitID, tr.Froutingname, a.FNeedQty FQty,--a.famount, b.FErpClsID,isnull(e.Fname,'') FMaterielType, d.FBomNumber,d.FNote-- ,-- isnull(i.fname,'') FUseStatus from #Mutidata1 a inner join t_icitem b on a.fitemid=b.fitemid left outer join t_item c on b.funitid=c.fitemid inner join icbom d on a.FBomInterid=d.finterid and a.FItemID=d.FItemID left outer join t_submessage e on b.FErpClsID=e.finterid inner join t_item h on b.fitemid=h.fitemid left outer join t_submessage i on b.fusestate=i.finterid left outer join t_routing Tr on b.fitemid =tr.fitemidwhere 1=1 order by FIndex desc update aset fsm= case when b.fstatus=0 then '该物料BOM未审核、未使用,请审核、使用后对其下级BOM进行检查(BOM NO Check and NO Use)' when b.fstatus=1 and b.fusestatus=1073 then '该物料BOM已审核、未使用,请使用后对其下级BOM进行检查 (BOM is Checked but NO Use)' else '正常(correct)' endfrom #test a,icbom bwhere a.fitemid=b.fitemid and a.fattid in(2,5)update aset fsm='该自制件没有BOM,请对该物料建立BOM,并将BOM审核、使用(This Material no BOM,Please create a new BOM , Check and Use this BOM)'from #test awhere a.fitemid not in (select distinct fitemid from icbom) and a.fattid in(2,5)update aset fsm=fsm+'--该物料存在两个或两个以上的BOM,请进行处理(This material have two or more BOM,Please check it)--'from #test awhere a.fitemid in (select fitemid from icbom group by fitemid having count(fitemid)>1)select FIndex,FitemID,FItemType, FBOMLevel ,FLevel as '层次(LEVEL)' ,fnumber as '物料代码(Number)',fname as '物料名称(Name)', fsm as '提示(Tips)',isnull(FModel,'') '规格(Spec.)',isnull(fdraw,'') '图号(Draw No.)',isnull(fmaterial,'') '材质(Material)',isnull(fdrawsize,'') '图幅(Size of Drawing)', isnull(funit,'') '单位(Unit)', frouting as '工艺路线(Routing)', fqty '用量(Qty)',--a.famount '材料成本(Cost)',isnull(fattname,'') '物料属性(Mtr.Att.)',fbomnumber 'BOM编号(BOM NUM.)',fremark as '备注(Remark)',FCZQualityCheck from #test adrop table #Errorssdrop table #Errorsdrop table #data1drop table #Mutidatadrop table #Mutidata1drop table #MutiParentItemdrop table #test