最近一直在搞BOM展开的工作,其实在生产制造企业都是最需要的. 这里放BOM展开的中间函数,提供给需要展开BOM写存储的一些思路.
 
BOM展开存储过程中间函数,其他的存储过程展开都需要调用此存储过程函数
执行:Exec BOMCOMMONRpt @Code,@Sort,@ItemCode,@Quantity,@Level out
需要传入变量Code:需要展开的物料代码
                      Sort:排序
                      ItemCode:第一层的源物料
                      Quantity:父物料数量
                      Level :层级
 
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[BOMCOMMONRpt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
Drop Procedure [dbo].[BOMCOMMONRpt]
GO
 
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
 
Create Procedure [dbo].[BOMCOMMONRpt]
(
       @ItemCode  nvarchar(40),
       @Sort     int,
       @Source nvarchar(50),
       @Quantity1 int,
       @Level   int   out
)
--With encryption
as
 
declare @LevCount int                     --LevelCount
declare @strSQL nvarchar(4000)       
declare @strSQL1 nvarchar(4000) 
declare @Father nvarchar(50)
declare @Code nvarchar(50)
declare @Quantity2 int
 
 
Set @strSQL = ''
Set @strSQL1 = ''
set @LevCount=@Level
 
Begin
 
--创建存储表
--    Create Table W_Temp1
--    (
--           IDNo int identity(1,1),                      --No
--           Father nvarchar(30)    ,                      --AcctCode
--           Code nvarchar(200)   ,                     --AcctName
--           Quantity int,
--           BOMLevel int,
--           SortID   int,
--           Source nvarchar(50),
--           BOMQnty int
--          
--    )
 
       --alter table W_Temp1 add BOMQnty int
 
       Declare @MyPrc Cursor
       Set @MyPrc = Cursor FOR
           Select  Father,Code,Quantity from itt1
           where Father =@ItemCode
      
       Open @MyPrc
       Fetch next from  @MyPrc Into @Father,@Code,@Quantity2
       While @@FETCH_STATUS = 0
       Begin
 
              --插入所有子件
 
              insert into W_Temp1 ( Father,Code,Quantity,BOMLevel,SortID,Source,BOMQnty) values (@Father,@Code,@Quantity2*@Quantity1,@LevCount,@Sort,@Source,@Quantity2)
                 
              Fetch next from  @MyPrc Into @Father,@Code,@Quantity2
 
       End
       Close @MyPrc
       Deallocate @MyPrc
 
       set @Level=@Level+1
 
 
End