--测试数据
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','小分市'
GOCREATE FUNCTION f_Pid(@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.PID,@Level
FROM tb a,@t_Level b
WHERE a.ID=b.ID
AND b.Level=@Level-1
END
RETURN
END
GO

解决方案 »

  1.   

    --测试数据   深度排序     
      DECLARE   @t   TABLE(ID   char(3),PID   char(3),Name   nvarchar(10))   
      INSERT   @t   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','小分市'   
        
      --深度排序显示处理   
      --生成每个节点的编码累计(相同当单编号法的编码)   
      DECLARE   @t_Level   TABLE(ID   char(3),Level   int,Sort   varchar(8000))   
      DECLARE   @Level   int   
      SET   @Level=0   
      INSERT   @t_Level   SELECT   ID,@Level,ID   
      FROM   @t   
      WHERE   PID   IS   NULL   
      WHILE   @@ROWCOUNT>0   
      BEGIN   
      SET   @Level=@Level+1   
      INSERT   @t_Level   SELECT   a.ID,@Level,b.Sort+a.ID   
      FROM   @t   a,@t_Level   b   
      WHERE   a.PID=b.ID   
      AND   b.Level=@Level-1   
      END   
        
      --显示结果   
      SELECT   a.*   
      FROM   @t   a,@t_Level   b   
      WHERE   a.ID=b.ID   
      ORDER   BY   b.Sort   
      /*--结果   
      ID       PID       Name                 
      ------   ---------   ----------     
      001     NULL   山东省   
      002     001       烟台市   
      004     002       招远市   
      003     001       青岛市   
      005     NULL   四会市   
      006     005       清远市   
      007     006       小分市   
      --*/--查询指定节点及其所有子节点的函数   
      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     招远市   
      --*/ --测试数据
    DECLARE @t TABLE(ID char(3),PID char(3),Name nvarchar(10))
    INSERT @t 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','小分市'--深度排序显示处理
    --生成每个节点的编码累计(相同当单编号法的编码)
    DECLARE @t_Level TABLE(ID char(3),Level int,Sort varchar(8000))
    DECLARE @Level int
    SET @Level=0
    INSERT @t_Level SELECT ID,@Level,ID
    FROM @t
    WHERE PID IS NULL
    WHILE @@ROWCOUNT>0
    BEGIN
        SET @Level=@Level+1
        INSERT @t_Level SELECT a.ID,@Level,b.Sort+a.ID
        FROM @t a,@t_Level b
        WHERE a.PID=b.ID
            AND b.Level=@Level-1
    END--显示结果
    SELECT SPACE(b.Level*2)+'|--'+a.Name
    FROM @t a,@t_Level b
    WHERE a.ID=b.ID
    ORDER BY b.Sort
    /*--结果
    |--山东省
      |--烟台市
        |--招远市
      |--青岛市
    |--四会市
      |--清远市
        |--小分市
    --*/
      

  2.   

    CREATE FUNCTION f_Pid(@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.PID,@Level
    FROM tb a,@t_Level b
    WHERE a.ID=b.ID
    AND b.Level=@Level-1
    END
    RETURN
    END
    GO
    --上面的用户定义函数可以处理一个节点有多个父节点的情况,对于标准的树形数据而言,由于每个节点仅有一个父节点,所以也可以通过下面的用户定义函数实现查找标准树形数据的父节点。
    CREATE FUNCTION f_Pid(@ID char(3))
    RETURNS @t_Level TABLE(ID char(3))
    AS
    BEGIN
    INSERT @t_Level SELECT @ID
    SELECT @ID=PID FROM tb
    WHERE ID=@ID
    AND PID IS NOT NULL
    WHILE @@ROWCOUNT>0
    BEGIN
    INSERT @t_Level SELECT @ID
    SELECT @ID=PID FROM tb
    WHERE ID=@ID
    AND PID IS NOT NULL
    END
    RETURN
    END
      

  3.   

    declare @idwith cte (id,parentid)
    as
    (
    select id,parentid from tb where id=@id
    union all
    select b.id,b.parentid 
    from cte A ,tb B where a.parentid = b.id
    )
    select id,parentid
    from cte
      

  4.   


    use tempdb
    gocreate table tb(id int, [Name] varchar(20),parentID int )
    insert into tb 
    select           382        , '调度员岗位',              -1 
    union all select 383        , '基础知识',                382 
    union all select 390        , '电工基础' ,               383 
    union all select 1995       , 'test知识点' ,             390create function f_cid(@id int)
    returns varchar(500)
    as
    begin
         declare @t table(id int,[name] varchar(30),parentid int,lev int)
         declare @lev int
         set @lev=1
         insert into @t select *,@lev from  tb where id=@id
         while(@@rowcount>0)
         begin
              set @lev=@lev+1
              insert into @t select a.*,@lev from tb a,@t b
              where a.parentid=b.id and b.lev=@lev-1
         end
         declare @cids varchar(500)
         select @cids=isnull(@cids+',','')+ltrim(id) from @t order by lev
         return @cids
    end
    godrop function f_cidselect *,ids=dbo.f_cid(id) from tb
    --得到每个节点路径:
    create proc test
    @id int
    as
    set nocount off
    select *,cast(' ' as varchar(50)) fullpath  into #os from tb
    DECLARE @i int,@j int
    set @i=0
    set @j=1
    select @i=max(parentid) from #os
    update #os set fullpath=id 
    while @j<=@i
    begin
           update #os set fullpath=a.fullpath+','+ltrim(#os.id) 
                from #os inner join #os a on #os.parentid=a.id 
           where #os.parentid=@j 
           set @j=@j+1
    end
    select * from #os
    go
    --调用存储过程
    exec test 1
    --------------------------------------
    382 调度员岗位 -1 382
    383 基础知识 382 382,383
    390 电工基础 383 382,383,390
    1995 test知识点 390 382,383,390,1995
      

  5.   


    declare @id int;with cte (id,parentid)
    as
    (
    select id,parentid from tb where id=@id
    union all
    select b.id,b.parentid 
    from cte A ,tb B where a.parentid = b.id
    )
    select id,parentid
    from cte
      

  6.   


     
    declare @id ;with cte (id,parentid) 
    as 

    select id,parentid from tb where id=@id 
    union all 
    select b.id,b.parentid 
    from cte A ,tb B where a.parentid = b.id 

    select id,parentid 
    from cte 
      

  7.   

    你这个我怎么查询??报错没有声明变量  ‘@t_Level’ 
      

  8.   

    CREATE FUNCTION f_Pid(@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.PID,@Level
    FROM tb a,@t_Level b
    WHERE a.ID=b.ID
    AND b.Level=@Level-1
    END
    RETURN
    END
    GO
    --上面的用户定义函数可以处理一个节点有多个父节点的情况,对于标准的树形数据而言,由于每个节点仅有一个父节点,所以也可以通过下面的用户定义函数实现查找标准树形数据的父节点。
    CREATE FUNCTION f_Pid(@ID char(3))
    RETURNS @t_Level TABLE(ID char(3))
    AS
    BEGIN
    INSERT @t_Level SELECT @ID
    SELECT @ID=PID FROM tb
    WHERE ID=@ID
    AND PID IS NOT NULL
    WHILE @@ROWCOUNT>0
    BEGIN
    INSERT @t_Level SELECT @ID
    SELECT @ID=PID FROM tb
    WHERE ID=@ID
    AND PID IS NOT NULL
    END
    RETURN
    END
      

  9.   

    with as 在2000里能用吗?
      

  10.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([ID] int,[Name] varchar(10),[ParentID] int)
    insert [tb]
    select 382,'调度员岗位',-1 union all
    select 383,'基础知识',382 union all
    select 390,'电工基础',383 union all
    select 1995,'test知识点',390
    go
    --select * from [tb]declare @id int
    set @id=1995
    --set @id=390
    select ID,Name,ParentID into # from tb where id=@id
    while @@rowcount>0
    insert # 
    select a.ID,a.Name,a.ParentID 
    from tb a join # b
    on a.ID=b.ParentID and a.ID not in(select ID from #)select * from #
    --@id=1995
    /*
    ID          Name       ParentID
    ----------- ---------- -----------
    390         电工基础       383
    383         基础知识       382
    382         调度员岗位      -1
    1995        test知识点    390(4 行受影响)
    */--@id=390
    /*
    ID          Name       ParentID
    ----------- ---------- -----------
    383         基础知识       382
    382         调度员岗位      -1
    390         电工基础       383(3 行受影响)
    */
    drop table #