27,579
社区成员
发帖
与我相关
我的任务
分享
create table #bom(母件 varchar(50),子件项次 varchar(100),子件 varchar (50),用量 numeric(16,4))
insert into #bom
select '10210100030','1','20012100008',1 union all
select '10210100030','2','20012110008',0.5 union all
select '10210100030','3','30300701001',1 union all
select '20012100008','1','2001210P161',1 union all
select '20012100008','2','30400201100',2 union all
select '20012110008','1','2001211S104',1 union all
select '20012110008','2','30400203000',0.5 union all
select '10210100026','1','20012100005',3 union all
select '10210100026','2','20012190012',4 union all
select '10210100026','3','30300201001',1 union all
select '20012100005','1','2001210P162',1 union all
select '20012100005','2','30400201100',0.5 union all
select '20012190012','1','2001219Z001',2 union all
select '2001219Z002','1','30400201111',2 union all
select '20012190012','2','2001219Z002',2
;
with list as(
select 产品=母件,母件,子件,子件项次,用量
,paths=cast(母件+'\'+子件 as varchar(4000))
from #bom
where 母件 in('10210100026','20012100008')
union all
select b.产品,a.母件,a.子件,a.子件项次,a.用量
,paths=cast(b.paths+'\'+a.子件 as varchar(4000))
from #bom a
inner join list b on a.母件=b.子件
)
select *
from list
order by paths
/*
产品 母件 子件 子件项次 用量 paths
-------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ---------------------------------------------------------------------------------------------------- --------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
10210100026 10210100026 20012100005 1 3.0000 10210100026\20012100005
10210100026 20012100005 2001210P162 1 1.0000 10210100026\20012100005\2001210P162
10210100026 20012100005 30400201100 2 0.5000 10210100026\20012100005\30400201100
10210100026 10210100026 20012190012 2 4.0000 10210100026\20012190012
10210100026 20012190012 2001219Z001 1 2.0000 10210100026\20012190012\2001219Z001
10210100026 20012190012 2001219Z002 2 2.0000 10210100026\20012190012\2001219Z002
10210100026 2001219Z002 30400201111 1 2.0000 10210100026\20012190012\2001219Z002\30400201111
10210100026 10210100026 30300201001 3 1.0000 10210100026\30300201001
20012100008 20012100008 2001210P161 1 1.0000 20012100008\2001210P161
20012100008 20012100008 30400201100 2 2.0000 20012100008\30400201100
(10 行受影响)
*/