日期:2014-05-18 浏览次数:20533 次
if object_id('[BOM]') is not null drop table [BOM] go create table [BOM] (CO_CD int,ITEMPARENT_CD nvarchar(40),BOM_SQ int,CHILD_CD nvarchar(40),QTY int) insert into [BOM] select 1000,'10C-AD-AT-NAC11-P',1,'A30C-SAN0752N01-(14)',1 union all select 1000,'10C-AD-AT-NAC11-P',2,'A055-CA2AUS-L',1 union all select 1000,'10C-AD-AT-NAC11-P',3,'A061-AD-AT-NAC11-L',1 union all select 1000,'10C-AD-AT-NAC11-P',4,'A061-AD-AT-NAC11-IBL',1 union all select 1000,'10C-AD-AT-NAC11-P',5,'A068-AD-00-AC-1620',1 union all select 1000,'A30C-SAN0752N01-(14)',1,'A056-AD-00-AC-1620',1 union all select 1000,'A30C-SAN0752N01-(14)',2,'A054-AD-00-AC-1620',1 union all select 1000,'A30C-SAN0752N01-(14)',3,'A053-AD-00-AC-1620',1 union all select 1000,'A30C-SAN0752N01-(14)',4,'A052-AD-00-AC-1620',1 union all select 1000,'A30C-SAN0752N01-(14)',5,'A051-AD-00-AC-1620',1 if object_id('[mono]') is not null drop table [mono] go create table [mono] (CO_CD int,ITEM_CD nvarchar(40),ITEM_NM nvarchar(6)) insert into [mono] select 1000,'10C-AD-AT-NAC11-P','AAA' union all select 1000,'A30C-SAN0752N01-(14)','BBB' select * from [BOM] select * from [mono] declare @i nvarchar(50) set @i ='10C-AD-AT-NAC11-P'; with PCTE as( select 1 as lvl,bom_sq,CHILD_CD ,QTY from BOM where ITEMPARENT_CD = @i union all select PCTE.lvl +1,N.bom_sq,N.CHILD_CD,PCTE.QTY*N.QTY as qty from PCTE inner join BOM as N on PCTE.child_cd = N.ITEMPARENT_CD) select * from PCTE /* lvl bom_sq CHILD_CD QTY ----------- ----------- ---------------------------------------- ----------- 1 1 A30C-SAN0752N01-(14) 1 1 2 A055-CA2AUS-L 1 1 3 A061-AD-AT-NAC11-L 1 1 4 A061-AD-AT-NAC11-IBL 1 1 5 A068-AD-00-AC-1620 1 2 1 A056-AD-00-AC-1620 1 2 2 A054-AD-00-AC-1620 1 2 3 A053-AD-00-AC-1620 1 2 4 A052-AD-00-AC-1620 1 2 5 A051-