if object_id('[tb]') is not null drop table [tb]gocreate table [tb]([modeid] int,modename varchar(20),parentid int)insert [tb]select 100 ,'商品管理', 0 union allselect 101 ,'定单管理', 0 union allselect 102 ,'用户管理', 0 union allselect 104 ,'学院广告', 0 union allselect 105 ,'系统设置', 0 union allselect 106 ,'附件管理', 0 union allselect 107 ,'商品管理', 100 union allselect 108 ,'明细管理', 100 union allselect 109 ,'物流管理', 100 union allselect 110 ,'商品信息管理', 107 union allselect 111 ,'商品分类管理', 107 union allselect 112 ,'回收站管理', 107 union allselect 114 ,'团购管理', 108 union allselect 115 ,'拍卖管理', 108 union allselect 116 ,'优惠管理', 108 union allselect 117 ,'会员管理', 102 union allselect 118 ,'会员卡管理', 102 union allselect 119 ,'资金管理', 102 union allselect 120 ,'管理员管理', 102 union allselect 121 ,'添加管理员', 120 union allselect 122 ,'修改管理员', 120go--查所有子结点if object_id('f_getC') is not null drop function f_getCgocreate function f_getC(@id int) returns @re table(id int,level int,sort varchar(10)) as begin declare @l int set @l=0 insert @re select @id,@l,null while @@rowcount>0 begin set @[email protected]+1 insert @re select a.modeid,@l,ltrim(isnull(b.sort,a.modeid)) from tb as a,@re as b where b.id=a.parentid and [email protected] end update @re set level = level -1 return end go select a.modeid,a.parentid,REPLICATE(' ',b.level) +'┝'+a.modename,b.level,b.sort from tb a,f_getC(0) b where a.modeid=b.id order by case when b.level<2 then 0 else 1 end,b.sort,b.level/*modeid parentid sort level ----------- ----------- -------------------------------------------------- ---------- -----------100 0 ┝商品管理 100 0107 100 ┝商品管理 100 1108 100 ┝明细管理 100 1109 100 ┝物流管理 100 1101 0 ┝定单管理 101 0102 0 ┝用户管理 102 0117 102 ┝会员管理 102 1118 102 ┝会员卡管理 102 1119 102 ┝资金管理 102 1120 102 ┝管理员管理 102 1104 0 ┝学院广告 104 0105 0 ┝系统设置 105 0106 0 ┝附件管理 106 0110 107 ┝商品信息管理 100 2111 107 ┝商品分类管理 100 2112 107 ┝回收站管理 100 2114 108 ┝团购管理 100 2115 108 ┝拍卖管理 100 2116 108 ┝优惠管理 100 2121 120 ┝添加管理员 102 2122 120 ┝修改管理员 102 2(所影响的行数为 21 行)*/--查所有子结点,带路径与排序if object_id('f_getC') is not null drop function f_getCgocreate function f_getC(@id int) returns @re table(id int,level int,sort varchar(100),path varchar(500)) as begin declare @l int set @l=0 insert @re select [modeid],@l,right('00000'+ltrim(modeid),5),modename from tb where [email protected] while @@rowcount>0 begin set @[email protected]+1 insert @re select a.modeid,@l,b.sort+right('00000'+ltrim(a.modeid),5), b.path+' - '+a.modename from tb as a,@re as b where b.id=a.parentid and [email protected] end update @re set level = level return end go select a.modeid,a.parentid,REPLICATE(' ',b.level) +'┝'+a.modename,b.level,b.sort ,b.path from tb a,f_getC(0) b where a.modeid=b.id order by sort/*modeid parentid level ----------- ----------- -------------------- ----------- -------------------- ----------------------------------------100 0 ┝商品管理 0 00100 商品管理107 100 ┝商品管理 1 0010000107 商品管理 - 商品管理110 107 ┝商品信息管理 2 001000010700110 商品管理 - 商品管理 - 商品信息管理111 107 ┝商品分类管理 2 001000010700111 商品管理 - 商品管理 - 商品分类管理112 107 ┝回收站管理 2 001000010700112 商品管理 - 商品管理 - 回收站管理108 100 ┝明细管理 1 0010000108 商品管理 - 明细管理114 108 ┝团购管理 2 001000010800114 商品管理 - 明细管理 - 团购管理115 108 ┝拍卖管理 2 001000010800115 商品管理 - 明细管理 - 拍卖管理116 108 ┝优惠管理 2 001000010800116 商品管理 - 明细管理 - 优惠管理109 100 ┝物流管理 1 0010000109 商品管理 - 物流管理101 0 ┝定单管理 0 00101 定单管理102 0 ┝用户管理 0 00102 用户管理117 102 ┝会员管理 1 0010200117 用户管理 - 会员管理118 102 ┝会员卡管理 1 0010200118 用户管理 - 会员卡管理119 102 ┝资金管理 1 0010200119 用户管理 - 资金管理120 102 ┝管理员管理 1 0010200120 用户管理 - 管理员管理121 120 ┝添加管理员 2 001020012000121 用户管理 - 管理员管理 - 添加管理员122 120 ┝修改管理员 2 001020012000122 用户管理 - 管理员管理 - 修改管理员104 0 ┝学院广告 0 00104 学院广告105 0 ┝系统设置 0 00105 系统设置106 0 ┝附件管理 0 00106 附件管理(21 行受影响)*/----------
详细解决方案
SQL Server2000递归查询(十分棒)
热度:117 发布时间:2016-05-05 14:48:23.0
相关解决方案
- sql server2000 ntext字段乱码有关问题
- asp.net怎的连接SQL server2000
- 高手帮忙解决向SQL SERVER2000 提交DateTime类型数据的有关问题.
- foxpro数据库导入sql server2000
- []ColdFusion Administrator 连接 Sql server2000 报错
- [求助]请发SQL Server2000 JDBC驱动下载地址
- 请问SQL Server2000 SP4补丁安装路径
- 求助:如何安装sql server2000?
- ASP 转换时间存入SQL Server2000 数据库解决方案
- hibernate + sql server2000 数据库连接有关问题
- Servlet连接SQL Server2000 连接不上,请大家帮帮忙!多谢。
- sql server2000+jdk1.5+tomcat5.20连接数据库的有关问题
- sql server2000 中的%解决方法
- jsp操作数据库出错(server2000),该如何处理
- 高分求 jsp sql server2000~b/s进销存管理系统
- 数据库连接池有关问题(Tomcat5.0.18+SQL Server2000)
- sql server2000 jdbc连接有关问题~大家帮忙
- jsp+tomcat+sql server2000 无法插入中文,在线急等!解决方法
- data truncation错误 ibatis,Sql server2000
- MyEclipse6.5在本机上运行项目时,连接sql server2000,出现让人蛋疼的有关问题
- eclipse怎么配饰sql server2000 的桥联,请各位学长指教
- eclips连接SQL Server2000,该怎么解决
- sql server2000 和eclipse连接出错解决思路
- 查询结果分类有关问题,SQL SERVER2000
- sql server2000 不同版本其间可以覆盖master吗
- sql server2000 运行一段时间置疑,重启后正常,反复出现
- 请各位大侠指教 JDBC连接SQL Server2000,该如何解决
- sql server2000 与java连接有关问题
- 请问sql server2000+tomcat5.0数据库连接池有关问题
- 小弟我通过odbc数据源连接sql server2000 求救!