一: bom展开(按任一父结点展开到最底层)
以下写一个简单的,视具体要求稍做修改即可。
create table 表(levelid int,levelname char(2),parent int)
insert 表 select 1, 'aa' , 0
union all select 2 , 'bb' , 1
union all select 3 , 'cc' , 1
union all select 4 , 'dd' , 2
union all select 5 , 'ee' , 3
union all select 6 , 'ff', 5
create function bom (@name char(2))
returns @tb table (levelid int,levelname char(2),parent int)
as
begin
insert @tb select levelid,levelname,parent from 表 where levelname = @name
while @@rowcount > 0
insert @tb select levelid,levelname,parent from 表
where parent in (select levelid from @tb)
and levelid not in (select levelid from @tb)
return
end
select * from dbo.bom('bb')
levelid levelname parent
----------- --------- -----------
2 bb 1
4 dd 2
(所影响的行数为 2 行)
二: llc(最低层码)
1:物料主文件中至少有这两个字段
itemno,llc
2:bom中至少有这两个字段 (树状)
parentitem,itemno
3:
create procedure llc
as
update 物料主文件 set llc = 0 --先将llc全部清为0
declare @i tinyint
set @i = 0
while @i <= 10 -- 假设bom最多只有10层
begin
update a set a.llc = @i + 1 --子结点的llc加1
from 物料主文件 a
join bom b on a.itemno = b.itemno
join 物料主文件 c on c.itemno = b.parentitem
where c.llc = @i
set @i = @i + 1
end
/*********** usage: exec llc *******/