CREATE TABLE BOM(PID INT,ID INT)
INSERT INTO BOM SELECT 801,101
INSERT INTO BOM SELECT 801,102
INSERT INTO BOM SELECT 801,103
INSERT INTO BOM SELECT 801,601
INSERT INTO BOM SELECT 601,101
INSERT INTO BOM SELECT 601,105
INSERT INTO BOM SELECT 601,501
INSERT INTO BOM SELECT 501,106
INSERT INTO BOM SELECT 501,121
GOCREATE FUNCTION F_GETROOT(@PID INT)
RETURNS INT
AS
BEGIN
    DECLARE @ID INT
    WHILE EXISTS(SELECT 1 FROM BOM WHERE ID=@PID)
    BEGIN
        SET @ID=@PID
        SELECT @PID=PID FROM BOM WHERE ID=@ID
    END
    RETURN @PID
END
GOSELECT PID=DBO.F_GETROOT(PID),ID FROM BOM
GO/*
PID         ID
----------- ----------- 
801         101
801         102
801         103
801         601
801         101
801         105
801         501
801         106
801         121
*/
DROP FUNCTION F_GETROOT
DROP TABLE BOM
GO
--生成测试数据
create table BOM_1(Item int,bom_head varchar(20),bom_child varchar(20),number int,products_attribute  varchar(20))
insert into BOM_1 select 1 ,'A' ,'A1',1,'采购'
insert into BOM_1 select 2 ,'A' ,'A2',2,'生产'
insert into BOM_1 select 3 ,'A2','A3',3,'生产'
insert into BOM_1 select 4 ,'A2','A4',2,'采购'
insert into BOM_1 select 5 ,'A3','A5',2,'采购'
insert into BOM_1 select 6 ,'A3','A6',1,'采购'
insert into BOM_1 select 7 ,'B' ,'B1',1,'采购'
insert into BOM_1 select 8 ,'B' ,'B2',2,'生产'
insert into BOM_1 select 9 ,'B2','B3',3,'生产'
insert into BOM_1 select 10,'B2','B4',2,'采购'
insert into BOM_1 select 11,'B3','B5',2,'采购'
insert into BOM_1 select 12,'B3','B6',2,'采购'
go
   --创建用户定义函数,用于取每个父节点下子节点的采购配置信息
create function f_stock(@bom_head varchar(20))
returns @t table(bom varchar(20),number int)
as
begin 
    declare @level int
    declare @a table(bom varchar(20),number int,products_attribute varchar(20),[level] int)
    set @level=1    if exists(select 1 from BOM_1 where bom_head=@bom_head)    
    insert into @a 
    select bom_child,number,products_attribute,@level 
    from BOM_1 
    where bom_head=@bom_head
    
    while exists(select 1 from @a where [level]=@level and products_attribute='生产')
    begin
        set @level=@level+1
        insert into @a(bom,number,products_attribute,[level])
        select a.bom_child,a.number,a.products_attribute,@level 
        from BOM_1 a,@a b
        where a.bom_head=b.bom and b.[level]=@level-1
    end
    
    insert into @t(bom,number) select bom,number from @a where products_attribute='采购'
    return
end
go
--执行调用,取父节点'A'一个标准配置分解的采购信息及数量
select * from dbo.f_stock('A')
--生成测试数据
create table BOM(ID INT,PID INT,MSG VARCHAR(1000))
insert into BOM select 1,0,NULL
insert into BOM select 2,1,NULL
insert into BOM select 3,1,NULL
insert into BOM select 4,2,NULL
insert into BOM select 5,3,NULL
insert into BOM select 6,5,NULL
insert into BOM select 7,6,NULL
go--创建用户定义函数用于取每个父节点下子节点的采购配置信息
create function f_getChild(@ID VARCHAR(10))
returns @t table(ID VARCHAR(10),PID VARCHAR(10),Level INT)
as
begin
    declare @i int
    set @i = 1
    insert into @t select ID,PID,@i from BOM where PID = @ID
    
    while @@rowcount<>0
    begin
        set @i = @i + 1
        
        insert into @t 
        select 
            a.ID,a.PID,@i 
        from 
            BOM a,@t b 
        where 
            a.PID=b.ID and b.Level = @i-1
    end
    return
end
go--执行查询
select ID from dbo.f_getChild(3)
go--输出结果
/*
ID
----
5
6
7
*/--删除测试数据
drop function f_getChild
drop table BOM

解决方案 »

  1.   

    [code=SQL]CREATE TABLE BOM(PID INT,ID INT)
    INSERT INTO BOM SELECT 801,101
    INSERT INTO BOM SELECT 801,102
    INSERT INTO BOM SELECT 801,103
    INSERT INTO BOM SELECT 801,601
    INSERT INTO BOM SELECT 601,101
    INSERT INTO BOM SELECT 601,105
    INSERT INTO BOM SELECT 601,501
    INSERT INTO BOM SELECT 501,106
    INSERT INTO BOM SELECT 501,121
    GOCREATE FUNCTION F_GETROOT(@PID INT)
    RETURNS INT
    AS
    BEGIN
        DECLARE @ID INT
        WHILE EXISTS(SELECT 1 FROM BOM WHERE ID=@PID)
        BEGIN
            SET @ID=@PID
            SELECT @PID=PID FROM BOM WHERE ID=@ID
        END
        RETURN @PID
    END
    GOSELECT PID=DBO.F_GETROOT(PID),ID FROM BOM
    GO/*
    PID         ID
    ----------- ----------- 
    801         101
    801         102
    801         103
    801         601
    801         101
    801         105
    801         501
    801         106
    801         121
    */
    DROP FUNCTION F_GETROOT
    DROP TABLE BOM
    GO
    --生成测试数据
    create table BOM_1(Item int,bom_head varchar(20),bom_child varchar(20),number int,products_attribute  varchar(20))
    insert into BOM_1 select 1 ,'A' ,'A1',1,'采购'
    insert into BOM_1 select 2 ,'A' ,'A2',2,'生产'
    insert into BOM_1 select 3 ,'A2','A3',3,'生产'
    insert into BOM_1 select 4 ,'A2','A4',2,'采购'
    insert into BOM_1 select 5 ,'A3','A5',2,'采购'
    insert into BOM_1 select 6 ,'A3','A6',1,'采购'
    insert into BOM_1 select 7 ,'B' ,'B1',1,'采购'
    insert into BOM_1 select 8 ,'B' ,'B2',2,'生产'
    insert into BOM_1 select 9 ,'B2','B3',3,'生产'
    insert into BOM_1 select 10,'B2','B4',2,'采购'
    insert into BOM_1 select 11,'B3','B5',2,'采购'
    insert into BOM_1 select 12,'B3','B6',2,'采购'
    go
       --创建用户定义函数,用于取每个父节点下子节点的采购配置信息
    create function f_stock(@bom_head varchar(20))
    returns @t table(bom varchar(20),number int)
    as
    begin 
        declare @level int
        declare @a table(bom varchar(20),number int,products_attribute varchar(20),[level] int)
        set @level=1    if exists(select 1 from BOM_1 where bom_head=@bom_head)    
        insert into @a 
        select bom_child,number,products_attribute,@level 
        from BOM_1 
        where bom_head=@bom_head
        
        while exists(select 1 from @a where [level]=@level and products_attribute='生产')
        begin
            set @level=@level+1
            insert into @a(bom,number,products_attribute,[level])
            select a.bom_child,a.number,a.products_attribute,@level 
            from BOM_1 a,@a b
            where a.bom_head=b.bom and b.[level]=@level-1
        end
        
        insert into @t(bom,number) select bom,number from @a where products_attribute='采购'
        return
    end
    go
    --执行调用,取父节点'A'一个标准配置分解的采购信息及数量
    select * from dbo.f_stock('A')
    --生成测试数据
    create table BOM(ID INT,PID INT,MSG VARCHAR(1000))
    insert into BOM select 1,0,NULL
    insert into BOM select 2,1,NULL
    insert into BOM select 3,1,NULL
    insert into BOM select 4,2,NULL
    insert into BOM select 5,3,NULL
    insert into BOM select 6,5,NULL
    insert into BOM select 7,6,NULL
    go--创建用户定义函数用于取每个父节点下子节点的采购配置信息
    create function f_getChild(@ID VARCHAR(10))
    returns @t table(ID VARCHAR(10),PID VARCHAR(10),Level INT)
    as
    begin
        declare @i int
        set @i = 1
        insert into @t select ID,PID,@i from BOM where PID = @ID
        
        while @@rowcount<>0
        begin
            set @i = @i + 1
            
            insert into @t 
            select 
                a.ID,a.PID,@i 
            from 
                BOM a,@t b 
            where 
                a.PID=b.ID and b.Level = @i-1
        end
        return
    end
    go--执行查询
    select ID from dbo.f_getChild(3)
    go--输出结果
    /*
    ID
    ----
    5
    6
    7
    */--删除测试数据
    drop function f_getChild
    drop table BOM
    [/code]
      

  2.   

    2005,参考
    ;with t as(select id,fatherid,z
               from tb
               where id=2
               union all
               select id,,fatherid,z
               from tb inner join t on t.fatherid=tb.id)
    select * from t
      

  3.   

    with t as(select ID,Fatherid,Bz 
    from test
    where ID=2
    union all
    select test.ID,test.Fatherid,test.Bz
    from test inner join t on t.Fatherid=test.ID)
    select * from t
    报如下错误信息。
    消息 530,级别 16,状态 1,第 1 行
    语句被终止。完成执行语句前已用完最大递归 100。
      

  4.   

    --测试数据
    CREATE TABLE tb(ID char(3),PID char(3),Name nvarchar(10))
    INSERT tb SELECT '001',NULL ,'山东省'
    UNION ALL SELECT '002','001','烟台市'
    UNION ALL SELECT '004','002','招远市'
    UNION ALL SELECT '003','001','青岛市'
    UNION ALL SELECT '005',NULL ,'四会市'
    UNION ALL SELECT '006','005','清远市'
    UNION ALL SELECT '007','006','小分市'
    GO--查询指定节点及其所有子节点的函数
    CREATE FUNCTION f_Cid(@ID char(3))
    RETURNS @t_Level TABLE(ID char(3),Level int)
    AS
    BEGIN
    DECLARE @Level int
    SET @Level=1
    INSERT @t_Level SELECT @ID,@Level
    WHILE @@ROWCOUNT>0
    BEGIN
    SET @Level=@Level+1
    INSERT @t_Level SELECT a.ID,@Level
    FROM tb a,@t_Level b
    WHERE a.PID=b.ID
    AND b.Level=@Level-1
    END
    RETURN
    END
    GO--调用函数查询002及其所有子节点
    SELECT a.*
    FROM tb a,f_Cid('002') b
    WHERE a.ID=b.ID
    /*--结果
    ID   PID  Name       
    ------ ------- ---------- 
    002  001  烟台市
    004  002  招远市
    --*/
      

  5.   

    2005无限递归
    http://blog.csdn.net/roy_88/archive/2008/01/15/2045842.aspx
      

  6.   

    试试
    select * into # from tb where pid = @id
    while @@rowcount<> 0 insert # select * from tb where pid in(select id from #)
    select * from #
    drop table #
      

  7.   

    select * into # from tb where pid = @id
    while @@rowcount<> 0 insert # select * from tb where pid in(select id from #) and id not in (select id from #)
    select * from #
    drop table #
      

  8.   

    if object_id('tempdb.dbo.#') is not null drop table #
    select top 150 id = identity(int, 1, 1), parent=cast(null as int) into # from sys.columns a, sys.columns b
    update # set parent = id-1
    ;with father as
    (
    select * from # where parent = 15
    union all
    select a.* from # a join father b on a.parent = b.id
    )
    select * from father option(maxrecursion 0) --> option(maxrecursion 0-32767), 0表示无限制递归次数
    /*
    (135 行受影响)
    */
      

  9.   

    -- > 测试数据: #2005
    if object_id('tempdb.dbo.#2005') is not null drop table #2005
    create table #2005 (ID tinyint,fatherid tinyint,bz varchar(1))
    insert into #2005
    select 1,1,'A' union all
    select 2,1,'B' union all
    select 3,1,'C' union all
    select 4,2,'D' union all
    select 5,2,'E' union all
    select 6,5,'F' union all
    select 7,5,'G' union all
    select 8,7,'H';--> 2005
    with father as
    (
    select * from #2005 where fatherid = 2
    union all
    select a.* from #2005 a join father b on a.fatherid = b.id
    )
    select * from father option(maxrecursion 0) --> option(maxrecursion 0-32767), 0表示无限递归, 如无必要最好不要设置为0--> 2000
    declare @Level int
    set @Level = 1
    if object_id('tempdb.dbo.#2000') is not null drop table #2000
    select *, Level = @Level into #2000 from #2005 where fatherid = 2
    while @@rowcount > 0
    begin
    set @Level = @Level + 1
    insert into #2000 select a.*, @Level from #2005 a join #2000 b on a.fatherid = b.id where b.Level = @Level - 1
    end
    select ID, fatherid, bz from #2000 order by Level/*
    ID   fatherid bz
    ---- -------- ----
    4    2        D
    5    2        E
    6    5        F
    7    5        G
    8    7        H
    */
      

  10.   

    对Limpire的回答120%的满意,竟然写出了2000和2005两种方式...太感激!
    分少,也感谢其他热心的TX