--测试--测试数据 create table tb(n_id int IDENTITY(1,1) NOT NULL,parent_id int,name nvarchar(50),[order] int) set identity_insert tb on insert tb(n_id,parent_id,name,[order]) select 2 ,5 ,'管理版面',4 union all select 3 ,5 ,'管理专栏',4 union all select 4 ,5 ,'系统参数',4 union all select 5 ,0 ,'管理员配置工具',4 union all select 6 ,0 ,'新闻管理',1 union all select 7 ,5 ,'修改密码',4 union all select 18,17,'产品类别管理',3 union all select 19,17,'产品资料管理',3 union all select 20,17,'添加产品',3 union all select 21,17,'审核产品资料',3 union all select 22,17,'产品品牌管理',3 union all select 24,6 ,'热点新闻',1 union all select 25,6 ,'国际新闻',1 union all select 26,6 ,'国内新闻',1 union all select 27,6 ,'体育新闻',1 union all select 33,6 ,'dfasf',1 union all select 34,5 ,'数据备份',4 union all select 35,0 ,'订单管理',5 union all select 42,0 ,'会员管理',6 union all select 47,42,'会员资料管理',6 union all select 48,42,'会员组管理',6 union all select 50,6 ,'评论管理',1 union all select 51,42,'用户审核',6 union all select 52,6 ,'JS调用',1 union all select 53,0 ,'产品管理',7 union all select 54,53,'产品资料管理',7 union all select 55,53,'添加产品资料',7 go--辅助排序的函数 create function f_id() returns @re table(n_id int primary key,level int,sid varchar(8000)) as begin declare @l int set @l=0 insert @re select n_id,@l,right(10000+n_id,4) from tb where parent_id=0 while @@rowcount>0 begin set @l=@l+1 insert @re select a.n_id,@l,b.sid+right(10000+a.parent_id,4) from tb a,@re b where a.parent_id=b.n_id and b.level=@l-1 end return end go--调用函数实现查询 select a.*,space(b.level*2)+a.name from tb a,f_id() b where a.n_id=b.n_id order by b.sid go--删除测试 drop table tb drop function f_id/*--测试结果n_id parent_id name order ----------- ----------- ------------------ --------------------------- 5 0 管理员配置工具 4 管理员配置工具 2 5 管理版面 4 管理版面 3 5 管理专栏 4 管理专栏 4 5 系统参数 4 系统参数 7 5 修改密码 4 修改密码 34 5 数据备份 4 数据备份 6 0 新闻管理 1 新闻管理 24 6 热点新闻 1 热点新闻 25 6 国际新闻 1 国际新闻 26 6 国内新闻 1 国内新闻 27 6 体育新闻 1 体育新闻 33 6 dfasf 1 dfasf 52 6 JS调用 1 JS调用 50 6 评论管理 1 评论管理 35 0 订单管理 5 订单管理 42 0 会员管理 6 会员管理 47 42 会员资料管理 6 会员资料管理 48 42 会员组管理 6 会员组管理 51 42 用户审核 6 用户审核 53 0 产品管理 7 产品管理 54 53 产品资料管理 7 产品资料管理 55 53 添加产品资料 7 添加产品资料(所影响的行数为 22 行) --*/
2,5,"管理版面",4
3,5,"管理专栏",4
4,5,"系统参数",4
5,0,"管理员配置工具",4
6,0,"新闻管理",1
7,5,"修改密码",4
18,17,"产品类别管理",3
19,17,"产品资料管理",3
20,17,"添加产品",3
21,17,"审核产品资料",3
22,17,"产品品牌管理",3
24,6,"热点新闻",1
25,6,"国际新闻",1
26,6,"国内新闻",1
27,6,"体育新闻",1
33,6,"dfasf",1
34,5,"数据备份",4
35,0,"订单管理",5
42,0,"会员管理",6
47,42,"会员资料管理",6
48,42,"会员组管理",6
50,6,"评论管理",1
51,42,"用户审核",6
52,6,"JS调用",1
53,0,"产品管理",7
54,53,"产品资料管理",7
55,53,"添加产品资料",7
create table tb(n_id int IDENTITY(1,1) NOT NULL,parent_id int,name nvarchar(50),[order] int)
set identity_insert tb on
insert tb(n_id,parent_id,name,[order])
select 2 ,5 ,'管理版面',4
union all select 3 ,5 ,'管理专栏',4
union all select 4 ,5 ,'系统参数',4
union all select 5 ,0 ,'管理员配置工具',4
union all select 6 ,0 ,'新闻管理',1
union all select 7 ,5 ,'修改密码',4
union all select 18,17,'产品类别管理',3
union all select 19,17,'产品资料管理',3
union all select 20,17,'添加产品',3
union all select 21,17,'审核产品资料',3
union all select 22,17,'产品品牌管理',3
union all select 24,6 ,'热点新闻',1
union all select 25,6 ,'国际新闻',1
union all select 26,6 ,'国内新闻',1
union all select 27,6 ,'体育新闻',1
union all select 33,6 ,'dfasf',1
union all select 34,5 ,'数据备份',4
union all select 35,0 ,'订单管理',5
union all select 42,0 ,'会员管理',6
union all select 47,42,'会员资料管理',6
union all select 48,42,'会员组管理',6
union all select 50,6 ,'评论管理',1
union all select 51,42,'用户审核',6
union all select 52,6 ,'JS调用',1
union all select 53,0 ,'产品管理',7
union all select 54,53,'产品资料管理',7
union all select 55,53,'添加产品资料',7
go--辅助排序的函数
create function f_id()
returns @re table(n_id int primary key,level int,sid varchar(8000))
as
begin
declare @l int
set @l=0
insert @re select n_id,@l,right(10000+n_id,4)
from tb
where parent_id=0
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.n_id,@l,b.sid+right(10000+a.parent_id,4)
from tb a,@re b
where a.parent_id=b.n_id and b.level=@l-1
end
return
end
go--调用函数实现查询
select a.*,space(b.level*2)+a.name
from tb a,f_id() b
where a.n_id=b.n_id
order by b.sid
go--删除测试
drop table tb
drop function f_id/*--测试结果n_id parent_id name order
----------- ----------- ------------------ ---------------------------
5 0 管理员配置工具 4 管理员配置工具
2 5 管理版面 4 管理版面
3 5 管理专栏 4 管理专栏
4 5 系统参数 4 系统参数
7 5 修改密码 4 修改密码
34 5 数据备份 4 数据备份
6 0 新闻管理 1 新闻管理
24 6 热点新闻 1 热点新闻
25 6 国际新闻 1 国际新闻
26 6 国内新闻 1 国内新闻
27 6 体育新闻 1 体育新闻
33 6 dfasf 1 dfasf
52 6 JS调用 1 JS调用
50 6 评论管理 1 评论管理
35 0 订单管理 5 订单管理
42 0 会员管理 6 会员管理
47 42 会员资料管理 6 会员资料管理
48 42 会员组管理 6 会员组管理
51 42 用户审核 6 用户审核
53 0 产品管理 7 产品管理
54 53 产品资料管理 7 产品资料管理
55 53 添加产品资料 7 添加产品资料(所影响的行数为 22 行)
--*/