http://blog.csdn.net/xluzhong/articles/326894.aspx

解决方案 »

  1.   

    create table menu(
    id     int,      --菜单id
    name   char(50), --菜单名称
    parent int      --父菜单id
    )
    insert into  menu select 
    0,  '爷爷' ,  null union all select 
    1,  '爸爸',   0    union all select 
    2,  '叔叔',   0    union all select 
    3,  '大姑',   0    union all select 
    4,  '小姑',   0    union all select 
    5,  '我',     1    union all select 
    6,  '亲弟',   1    union all select 
    7,  '我儿子', 5    union all select 
    8,  '我女儿', 5   
    declare @s varchar(8000)
    select @s=','+cast(id as varchar(20))+'' from menu where id=1
    while @@rowCount>0
    select @s=@s+','+cast(id as varchar)  from menu 
         where charindex(','+cast(id as varchar)+',',@s+',')=0
         and charindex(','+cast(parent as varchar)+',',@s+',')>0
    select * from menu where charindex(','+cast(id as varchar)+',',@s+',')>0
    drop table menu
    (所影响的行数为 9 行)id          name                                               parent      
    ----------- -------------------------------------------------- ----------- 
    1           爸爸                                                 0
    5           我                                                  1
    6           亲弟                                                 1
    7           我儿子                                                5
    8           我女儿                                                5(所影响的行数为 5 行)
      

  2.   

    create function f_getchildid(@id int)
    returns @re table(id int)
    as
    begin
    insert into @re select id from menu where parent=@id
    while @@rowcount>0
    insert into @re select a.id 
    from menu a inner join @re b on a.parent=b.id
    where a.id not in(select id from @re)
    return
    end
    go--调用示例,显示1的所有子.
    select a.* from menu a inner join dbo.f_getchildid(1) b on a.id=b.id
      

  3.   

    一个sql语句目前还不行,sql 20005据说可以了.
      

  4.   

    一条SQL,还不行,建个过程吧!
      

  5.   

    lishengyu(玉):
    谢谢,俺试试这个方法。
      

  6.   

    展开层次结构
    数据库经常存储层次信息。例如,下面的数据是全球一些地区的层次表示形式。这种表示形式并未清楚地显示出数据中隐含的结构。Parent                             Child                             
    ---------------------------------- ----------------------------------
    World                              Europe                            
    World                              North America                     
    Europe                             France                            
    France                             Paris                             
    North America                      United States                     
    North America                      Canada                            
    United States                      New York                          
    United States                      Washington                        
    New York                           New York City                     
    Washington                         Redmond                           而下面的示例则更容易解释:World
       North America
          Canada
          United States
             Washington
                Redmond
             New York
                New York City
       Europe
          France
             Paris下面的 Transact-SQL 过程将一个编码的层次展开到任意深度。尽管 Transact-SQL 支持递归,但是使用临时表作为堆栈来跟踪所有正在处理中的项目(已经开始但尚未结束),将更加有效。某个项目一旦处理完毕,将被从堆栈中删除。当发现新的项目时,这些项目将被添加到堆栈中。CREATE PROCEDURE expand (@current char(20)) as
    SET NOCOUNT ON
    DECLARE @level int, @line char(20)
    CREATE TABLE #stack (item char(20), level int)
    INSERT INTO #stack VALUES (@current, 1)
    SELECT @level = 1WHILE @level > 0
    BEGIN
       IF EXISTS (SELECT * FROM #stack WHERE level = @level)
          BEGIN
             SELECT @current = item
             FROM #stack
             WHERE level = @level
             SELECT @line = space(@level - 1) + @current
             PRINT @line
             DELETE FROM #stack
             WHERE level = @level
                AND item = @current
             INSERT #stack
                SELECT child, @level + 1
                FROM hierarchy
                WHERE parent = @current
             IF @@ROWCOUNT > 0
                SELECT @level = @level + 1
          END
       ELSE
          SELECT @level = @level - 1
    END -- WHILE输入参数 (@current) 表示层次中的开始位置。它还跟踪主循环中的当前项目。使用的两个局部变量分别是 @level(用于跟踪层次结构中的当前级别)和 @line(是用于构造缩进行的工作区)。 SET NOCOUNT ON 语句避免输出中夹杂每个 SELECT 产生的 ROWCOUNT 消息。使用层次中开始点的项目标识符来创建和整理临时表 #stack,而 @level 被设置为与之匹配。#stack 中的 level 列允许同一个项目出现在数据库的多个级别中。虽然这种情况不适用于该示例中的地理数据,但是它可以用于其它示例。在下面的示例中,当 @level 大于 0 时,该过程执行以下步骤: 如果当前级别 (@level) 的堆栈中有任何项目,该过程将选择其中一个,并称之为 @current。
    缩进项目 @level 空格,然后打印该项目。
    从堆栈中删除该项目以免重复处理它,然后将其所有子项目添加到堆栈的下一级 (@level + 1) 中。这是唯一使用层次表 (#stack) 的地方。 
    如果使用传统的编程语言,就必须找到每个子项目并将其逐个添加到堆栈中。而使用 Transact-SQL,只用一个语句就能找到并添加所有的子项目,以免又使用一个嵌套循环。如果有子项目 (IF @@ROWCOUNT > 0),则下降一级处理它们 (@level = @level + 1);否则,继续在当前级别上处理。
    最后,如果在当前级别的堆栈中没有待处理的项目,则返回到上一级,看上一级是否有待处理的项目 (@level = @level - 1)。当再没有上一级时,则展开完毕。 
      

  7.   

    lishengyu(玉),你的这个方法,太妙了!谢谢。也感谢其他人。
      

  8.   

    oracle 里面有START WITH ...........
    CONNECT BY PRIOR .....;SQL SERVER好像没有发现.  :(
      

  9.   

    建一个存储过程 ALTER  PROCEDURE  spGetTree (
    @ID int)
    as
    set nocount on
    declare @tmp table (Id int,ConText varchar(50),ParentID int,depth int)
    insert @tmp select * from menu where ID=@ID
    while exists
    (select 1 from menu a,@tmp b where a.ParentID=b.ID and a.ID not in (select ID from @tmp))
     insert @tmp 
     select a.* from  menu a,@tmp b where a.ParentID=b.ID and a.ID not in (select ID from @tmp)
    select * from @tmp