CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  基础类

LouisXIV(夜游神) 请进...............................

楼主owenbeckham()2006-07-04 15:39:46 在 MS-SQL Server / 基础类 提问

create   table   test  
  (  
  ID   int,  
  classname   varchar(10),  
  parentID   int  
  )  
  insert   into   test  
  select   1,'中国',0   union   all  
  select   2,'上海',1   union   all  
  select   3,'江西',1   union   all  
  select   4,'浙江',1   union   all  
  select   5,'江苏',1   union   all  
  select   6,'南昌',3   union   all  
  select   7,'杭州市',4   union   all  
  select   8,'九江',3   union   all  
  select   9,'温州',4  
  go  
  create   function   f_root(@a   int)  
  returns   int  
  as  
  begin  
  declare   @pid   int  
  declare   @return   varchar(10)  
  select   @pid=@a  
  select   @return=''  
  while   @pid>=1  
  begin  
  select   @return=rtrim(@pid)+@return  
  select   @pid=parentid   from   test   where   id=@a  
  select   @a=@pid  
  end  
  return   @return  
  end  
  go  
  select   id,classname  
  from   test  
  order   by   cast(dbo.f_root(id)as   varchar)  
   
  /*  
  1 中国  
  2 上海  
  3 江西  
  6 南昌  
  8 九江  
  4 浙江  
  7 杭州市  
  9 温州  
  5 江苏  
  */  
   
   
  这个还是有问题无通用性,我再加了几个大类,比如国家.显示就不对  
  比如我改成这样子,显示出来的就不对了  
   
   
   
  create   table   test  
  (  
  ID   int,  
  classname   varchar(10),  
  parentID   int  
  )  
  insert   into   test  
  select   1,'中国',0   union   all  
  select   2,'上海',1   union   all  
  select   3,'江西',1   union   all  
  select   4,'浙江',1   union   all  
  select   5,'江苏',1   union   all  
  select   6,'南昌',3   union   all  
  select   7,'杭州市',4   union   all  
  select   8,'九江',3   union   all  
  select   9,'温州',4   union   all  
  select   10,'英国'0   union   all  
   
  select   11,'法国'0   union   all  
  go  
  create   function   f_root(@a   int)  
  returns   int  
  as  
  begin  
  declare   @pid   int  
  declare   @return   varchar(10)  
  select   @pid=@a  
  select   @return=''  
  while   @pid>=1  
  begin  
  select   @return=rtrim(@pid)+@return  
  select   @pid=parentid   from   test   where   id=@a  
  select   @a=@pid  
  end  
  return   @return  
  end  
  go  
  select   id,classname  
  from   test  
  order   by   cast(dbo.f_root(id)as   varchar)  
   
  问题点数:20、回复次数:2Top

1 楼paoluo(一天到晚游泳的鱼)回复于 2006-07-04 15:45:13 得分 10

修改一下即可。  
   
  create   function   f_root(@a   int)  
  returns   varchar(10)  
  as  
  begin  
  declare   @pid   int  
  declare   @return   varchar(1000)  
  select   @pid=@a  
  select   @return=''  
  while   @pid>=1  
  begin  
  select   @return=rtrim(@pid)+','+@return  
  select   @pid=parentid   from   test   where   id=@a  
  select   @a=@pid  
  end  
  return   @return  
  end  
  go  
  select   id,classname,   dbo.f_root(id)  
  from   test  
  order   by   dbo.f_root(id)Top

2 楼paoluo(一天到晚游泳的鱼)回复于 2006-07-04 15:46:29 得分 10

create   table   test  
  (  
  ID   int,  
  classname   Nvarchar(10),  
  parentID   int  
  )  
  insert   into   test  
  select   1,N'中国',0   union   all  
  select   2,N'上海',1   union   all  
  select   3,N'江西',1   union   all  
  select   4,N'浙江',1   union   all  
  select   5,N'江苏',1   union   all  
  select   6,N'南昌',3   union   all  
  select   7,N'杭州市',4   union   all  
  select   8,N'九江',3   union   all  
  select   9,N'温州',4   union   all  
  select   10,N'英国',0   union   all  
  select   11,N'法国',0  
  go  
  create   function   f_root(@a   int)  
  returns   varchar(10)  
  as  
  begin  
  declare   @pid   int  
  declare   @return   varchar(1000)  
  select   @pid=@a  
  select   @return=''  
  while   @pid>=1  
  begin  
  select   @return=rtrim(@pid)+','+@return  
  select   @pid=parentid   from   test   where   id=@a  
  select   @a=@pid  
  end  
  return   @return  
  end  
  go  
  select   id,classname  
  from   test  
  order   by   dbo.f_root(id)  
  GO  
  Drop     function   f_root  
  Drop   table   test  
  --Result  
  /*  
  id classname  
  1 中国  
  2 上海  
  3 江西  
  6 南昌  
  8 九江  
  4 浙江  
  7 杭州市  
  9 温州  
  5 江苏  
  10 英国  
  11 法国  
  */Top

相关问题

关键词

得分解答快速导航

  • 帖主:owenbeckham
  • paoluo
  • paoluo

相关链接

  • SQL Server类图书

广告也精彩

反馈

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