bom_no                   itm             prd_no                     id_no 
R03.26.00.00.00001-> 1 R03.26.02.03.00001 R03.26.02.03.00001->
R03.26.00.00.00001-> 2 R03.26.02.03.00002 R03.26.02.03.00002->
R03.26.00.00.00001-> 3 R03.26.01.01.00001 R03.26.01.01.00001->
R03.26.00.00.00001-> 4 R03.26.03.01.00001 R03.26.03.01.00001->
R03.26.00.00.00001-> 5 R03.26.02.02.00001 NULL
R03.26.00.00.00001-> 6 R03.26.02.03.00038 NULL
R03.26.00.00.00001-> 7 R03.26.02.01.00001 NULL
R03.26.02.03.00001-> 1 R03.26.02.03.00003 NULL
R03.26.02.03.00001-> 2 R03.26.02.03.00004 NULL
R03.26.02.03.00001-> 3 R03.26.02.03.00005 NULL
R03.26.02.03.00001-> 4 R03.26.02.03.00006 NULL
R03.26.02.03.00001-> 5 R03.26.02.03.00007 R03.26.02.03.00007->
以上是部分数据,希望通过查询bom_no,if id_no is not null ,继续把id_no作为bom_no查询,直到id_no全部为空,最好是把属于子件放在父件下面。以上是商品物料表(bom表),怎么写呢

解决方案 »

  1.   


    create table bom(bom_no varchar(50),itm int,prd_no varchar(50),id_no varchar(50));
    insert into bom 
    select 'R03.26.00.00.00001->' ,1 ,'R03.26.02.03.00001', 'R03.26.02.03.00001->' union all
    select 'R03.26.00.00.00001->' ,2 ,'R03.26.02.03.00002', 'R03.26.02.03.00002->' union all
    select 'R03.26.00.00.00001->' ,3 ,'R03.26.01.01.00001', 'R03.26.01.01.00001->' union all
    select 'R03.26.00.00.00001->' ,4 ,'R03.26.03.01.00001', 'R03.26.03.01.00001->' union all
    select 'R03.26.00.00.00001->' ,5 ,'R03.26.02.02.00001', NULL union all
    select 'R03.26.00.00.00001->' ,6 ,'R03.26.02.03.00038', NULL union all
    select 'R03.26.00.00.00001->' ,7 ,'R03.26.02.01.00001', NULL union all
    select 'R03.26.02.03.00001->' ,1 ,'R03.26.02.03.00003', NULL union all
    select 'R03.26.02.03.00001->' ,2 ,'R03.26.02.03.00004', NULL union all
    select 'R03.26.02.03.00001->' ,3 ,'R03.26.02.03.00005', NULL union all
    select 'R03.26.02.03.00001->' ,4 ,'R03.26.02.03.00006', NULL union all
    select 'R03.26.02.03.00001->' ,5 ,'R03.26.02.03.00007', 'R03.26.02.03.00007->';--select * from bom;with cte as 
    (
    select *,rn=0 from bom where id_no is not null
    union all
    select a.*,rn=b.rn+1 from bom a join cte b on a.bom_no =b.id_no --where b.id_no is not null
    )
    select * from cte;
      

  2.   


    只要没有环路就行,有环路的你可以指定那个rn 小于多少的先出来,其实是路径path写错了。
    select * from cte where rn<100