# 简单的递归算法,该怎么解决

www.MyException.Cn  网友分享于：2013-06-16  浏览：8次

SQL code
```
PARENT(成品)            ITEM_CODE(半成品或物料)        BOM_UNIT
99-88551-01030           97-88551-01030                   PCS  ----此为最高层
97-88551-01030           96-88551-01010                   PCS
96-88551-01010           #8003042-01010                   G
96-88551-01010           #6300035-01010                   G
96-88551-01010           20-88551-01030                   PCS
20-88551-01030           21-00001-00010                   G
20-88551-01030           22-00424-01010                   G

98485-00001              97485-00001                      PCS  ----此为最高层
97485-00001              90485-00001                      PCS
90485-00001              #8000-01010                      G
90485-00001              20485-00001                      PCS
20485-00001              21485-00001                      PCS
20485-00001              22485-00001                      G

98-78496-SP417K          #98-78496-SP417K                 G       ----此为最高层
98-78496-SP417K          97-78496-SP417K                  PCS
97-78496-SP417K          96-78496-SP2027                  PCS
97-78496-SP417K          #7678496-00010                   G
97-78496-SP417K          #88-78496-SP2027                 G
96-78496-SP2027          94-78496-SP2027                  PCS
96-78496-SP2027          #87-78496-SP2027                 G
94-78496-SP2027          90-78496-01010                   PCS
90-78496-01010           87-02029-00020                   G
90-78496-01010           02-00002-01011                   G

```

1.当输入某个PARENT最高层号码比如98-78496-SP417K，得到它下面所有层次的记录：
SQL code
```
98-78496-SP417K          #98-78496-SP417K                 G
98-78496-SP417K          97-78496-SP417K                  PCS
97-78496-SP417K          96-78496-SP2027                  PCS
97-78496-SP417K          #7678496-00010                   G
97-78496-SP417K          #88-78496-SP2027                 G
96-78496-SP2027          94-78496-SP2027                  PCS
96-78496-SP2027          #87-78496-SP2027                 G
94-78496-SP2027          90-78496-01010                   PCS
90-78496-01010           87-02029-00020                   G
90-78496-01010           02-00002-01011                   G

```

------解决方案--------------------
SQL code
```create table BOM1(PARENT varchar(50),ITEM_CODE varchar(50),BOM_UNIT varchar(50))

insert into BOM1(PARENT,ITEM_CODE,BOM_UNIT)
values('99-88551-01030','97-88551-01030','PCS')
insert into BOM1(PARENT,ITEM_CODE,BOM_UNIT)
values('97-88551-01030','96-88551-01010','PCS')
insert into BOM1(PARENT,ITEM_CODE,BOM_UNIT)
values('96-88551-01010','#8003042-01010','G')
insert into BOM1(PARENT,ITEM_CODE,BOM_UNIT)
values('96-88551-01010','#6300035-01010','G')
insert into BOM1(PARENT,ITEM_CODE,BOM_UNIT)
values('96-88551-01010','20-88551-01030','PCS')
insert into BOM1(PARENT,ITEM_CODE,BOM_UNIT)
values('20-88551-01030','21-00001-00010','G')
insert into BOM1(PARENT,ITEM_CODE,BOM_UNIT)
values('20-88551-01030','22-00424-01010','G')

insert into BOM1(PARENT,ITEM_CODE,BOM_UNIT)
values('98-78496-SP417K','#98-78496-SP417K','G')
insert into BOM1(PARENT,ITEM_CODE,BOM_UNIT)
values('98-78496-SP417K','97-78496-SP417K','PCS')
insert into BOM1(PARENT,ITEM_CODE,BOM_UNIT)
values('97-78496-SP417K','96-78496-SP2027','PCS')
insert into BOM1(PARENT,ITEM_CODE,BOM_UNIT)
values('97-78496-SP417K','#7678496-00010','G')
insert into BOM1(PARENT,ITEM_CODE,BOM_UNIT)
values('97-78496-SP417K','#88-78496-SP2027','G')
insert into BOM1(PARENT,ITEM_CODE,BOM_UNIT)
values('96-78496-SP2027','94-78496-SP2027','PCS')
insert into BOM1(PARENT,ITEM_CODE,BOM_UNIT)
values('96-78496-SP2027','#87-78496-SP2027','G')
insert into BOM1(PARENT,ITEM_CODE,BOM_UNIT)
values('94-78496-SP2027','90-78496-01010','PCS')
insert into BOM1(PARENT,ITEM_CODE,BOM_UNIT)
values('90-78496-01010','87-02029-00020','G')
insert into BOM1(PARENT,ITEM_CODE,BOM_UNIT)
values('90-78496-01010','02-00002-01011','G')

insert into BOM1(PARENT,ITEM_CODE,BOM_UNIT)
values('98485-00001','97485-00001','PCS')
insert into BOM1(PARENT,ITEM_CODE,BOM_UNIT)
values('97485-00001','90485-00001','PCS')
insert into BOM1(PARENT,ITEM_CODE,BOM_UNIT)
values('90485-00001','#8000-01010','PCS')
insert into BOM1(PARENT,ITEM_CODE,BOM_UNIT)
values('90485-00001','20485-00001','PCS')
insert into BOM1(PARENT,ITEM_CODE,BOM_UNIT)
values('20485-00001','21485-00001','G')
insert into BOM1(PARENT,ITEM_CODE,BOM_UNIT)
values('20485-00001','22485-00001','G')
GO

DECLARE @PARENT NVARCHAR(50)
SET @PARENT='98-78496-SP417K'

DECLARE @lev INT
SET @lev=0

IF OBJECT_ID('Tempdb..#B') IS NOT NULL
DROP TABLE #B
SELECT * ,lev=0 INTO #B FROM BOM1 WHERE PARENT=@PARENT

WHILE @@rowcount>0
BEGIN
SET @lev=@lev+1
INSERT INTO #B
SELECT   a.*,
lev=b.lev+1  FROM BOM1 AS a
INNER JOIN #B AS b  on b.ITEM_CODE=a.PARENT AND b.lev=@lev-1

END
SELECT PARENT,ITEM_CODE,BOM_UNIT FROM #B ORDER BY lev

/*
PARENT    ITEM_CODE    BOM_UNIT
98-78496-SP417K    #98-78496-SP417K    G
98-78496-SP417K    97-78496-SP417K    PCS
97-78496-SP417K    96-78496-SP2027    PCS
97-78496-SP417K    #7678496-00010    G
97-78496-SP417K    #88-78496-SP2027    G
96-78496-SP2027    94-78496-SP2027    PCS
96-78496-SP2027    #87-78496-SP2027    G
94-78496-SP2027    90-78496-01010    PCS
90-78496-01010    87-02029-00020    G
90-78496-01010    02-00002-01011    G*/
```