The BOM program has always been an urgent task for secondary ERP developers.
Yifei's 7.05 system, after searching for information online for a period of time, combined with existing report requirements. Write a stored procedure.
Create procedure [DBO]. [sp_bom_expand] (@ itemno varchar (80 ))
As
Begin
-- Bom scale-up
-- @ Itemno: Query product number
-- Return: component number v1001, main product number v1002, level code v1004, level number v1005, level sign v1006, expansion sign v1007, composition v1011, base number v1012, Loss Rate % v1013
Declare @ item_child varchar (80), @ item_expand varchar (80), @ item_levelno int, @ count int
Declare @ batch_number numeric (18, 6)
Create Table # view1 (v1001 varchar (80), v1002 varchar (80), v1004 varchar (80) default '', v1005 int default 0, v1006 char (1) default '0', v1007 char (1) default '0'
, V1011 numeric (18, 6) default 0, v1012 numeric (18, 6) default 0, v1013 numeric (18, 6) default 0)
Select @ item_levelno = 1
Insert into # view1 (v1001, v1002, v1004, v1005, v1011, v1012, v1013)
Select md003, @ itemno, md002, 1, md006, md007 * mc004, md008 from bommc, bommd where bommc. mc001 = bommd. md001 and mc001 = @ itemno
Declare bomexpand cursor for select v1001, v1004, v1005, v1011/v1012 from # view1 where v1007 = '0' for update
Open bomexpand/* Open the cursor */
Fetch next from bomexpand into @ item_child, @ item_expand, @ item_levelno, @ batch_number
While @ fetch_status = 0/* use a while loop to control cursor activity */
Begin
-- Select @ item_levelno, @ item_child
Update # view1 set v1007 = '1' where current of bomexpand
Select @ COUNT = count (*) from bommc, bommd where bommc. mc001 = bommd. md001 and mc001 = @ item_child
If @ COUNT = 0
Update # view1 set v1006 = '1' where current of bomexpand
Else
Begin
Insert into # view1 (v1001, v1002, v1004, v1005, v1011, v1012, v1013)
Select md003, @ item_child, @ item_expand + '. '+ md002, @ item_levelno + 1, md006 * @ batch_number, md007 * mc004, md008 from bommc, bommd where bommc. mc001 = bommd. md001 and mc001 = @ item_child
End
Fetch next from bomexpand into @ item_child, @ item_expand, @ item_levelno, @ batch_number/* other row data will be read in the loop body */
End
Close bomexpand/* close the cursor */
Deallocate bomexpand/* Delete cursor */
Select space (v1005 * 1) + '. '+ Cast (v1005 as varchar) as class, v1002 as main component no., v1001 as component no., case when mb025 = 'P' then' procurement'
When mb025 = 'M' then 'homemade parts'
When mb025 ='s 'then' outsourced work parts'
When mb025 = 'y' then 'virtual parts'
Else 'configuration parts' end as product number attribute,
Mb002 product name, mb003 specification, mb004 as unit, v1011 as composition, v1012 as base number, v1013 as loss rate, v1011/v1012 * (1 + v1013) as accumulative composition consumption from # view1 inner join invmb on v1001 = mb001
Order by v1004
End
Go
The effect is as follows:
System Standard Report
Tieguanyin-factory direct sales