CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
IBM Rational 系统开发最佳实践工具包 WebSphere MQ 最佳实践 TOP 15
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  基础类

遍历问题,可以用Sql实现么?深度遍历优先!

楼主bfwqc(丢在风中)2006-03-17 15:31:12 在 MS-SQL Server / 基础类 提问

id             title         pid  
  0101 a 0  
  0102 b 0101  
  0103 c 0101  
  0104 d 0102  
  0105 e 0103  
  0106 f 0104  
  0107 g 0103  
  0108 h 0103  
  0109 i 0108  
   
  输入0101  
  要得到结果:  
  a   b   d   f  
  a   c   e    
  a   c   g  
  a   c   h   i 问题点数:100、回复次数:5Top

1 楼terence4444(T4)回复于 2006-03-17 15:52:19 得分 1

建个存储过程   然后递归就可以了Top

2 楼jilate()回复于 2006-03-17 16:00:52 得分 1

好像看不到什么规律啊Top

3 楼jiezi316(大头仔)回复于 2006-03-17 16:13:27 得分 5

取其所有的子类:  
  create   procedure   up_select  
  @id  
  as  
  declare   @restb   table(id   int)  
  insert   into     @restb(id)   select   @id  
  while   exists(select   top   1   1   from   xx   where   pid   in(select   id   from     @restb)   and   id   not   in   (select   id   from     @restb))  
  begin  
  insert   into     @restb   select   id   from   xx  
  where   pid   in(select   id   from     @restb)   and   id   not   in   (select   id   from     @restb)  
  end  
  select   title   from   @restbTop

4 楼bfwqc(丢在风中)回复于 2006-03-17 16:23:26 得分 0

楼上的   我要的是  
  a   b   d   f  
  a   c   e    
  a   c   g  
  a   c   h   iTop

5 楼scmail81(琳·风の狼(修罗))回复于 2006-03-17 16:48:48 得分 93

create   table   A  
  (  
        id   varchar(10),  
        title   varchar(1),  
        pid   varchar(10)  
  )  
   
   
  insert   A   select   '0101','a','0'  
  insert   A   select   '0102','b','0101'  
  insert   A   select   '0103','c','0101'  
  insert   A   select   '0104','d','0102'  
  insert   A   select   '0105','e','0103'  
  insert   A   select   '0106','f','0104'  
  insert   A   select   '0107','g','0103'  
  insert   A   select   '0108','h','0103'  
  insert   A   select   '0109','i','0108'  
   
  create   Function   Find_txt(@id   varchar(10))  
  returns   @A   table(T_txt   varchar(1000))  
  as  
  begin  
  declare   @T   table(id   varchar(10),title   varchar(1),pid   varchar(10),   lev   int,T_txt   varchar(1000))  
  declare   @lev   int  
  set   @lev=1  
  insert   @T   select   id,title,pid,@lev,title   from   A   where   id=@id  
  while   @@rowcount>0  
  begin  
      set   @lev=@lev+1      
      insert   @T   select   A1.id,A1.title,A1.pid,@lev,   T.T_txt+   '   '   +   A1.title   from   A   A1,   (select   *   from   @T   where   lev=@lev-1)   T   where   T.id=A1.pid  
  end  
  insert   @A   select   T_txt   from   @T   T   where   (select   count(1)   from   @T   where   charindex(T.T_txt,T_txt)>0)=1  
  return    
  end  
   
  select   *   from   dbo.Find_txt('0101')Top

相关问题

  • 关于树的宽度优先遍历(给magic_jiang (magic) )
  • stl 中的优先级队列能够遍历么?
  • 一般树的广度优先遍历问题,急!急!急!
  • sql server2000中如何遍历列名?
  • 如何遍历一个sql表
  • 500分求深度优先和宽度优先算法~
  • 500分求深度优先和宽度优先算法~
  • 【求助】深度优先和广度优先的优缺点
  • 关于深度优先算法
  • SQL编程怎样遍历查询结果?

关键词

  • restb
  • insert
  • pid
  • varchar
  • title
  • select

得分解答快速导航

  • 帖主:bfwqc
  • terence4444
  • jilate
  • jiezi316
  • scmail81

相关链接

  • SQL Server类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo