无限级存储过程

wzqing 2009-03-23 09:37:02
表Area
字段
ID,Name,order_by,father_ID

是个无限级的菜单,请帮忙写一个存储过程,
是在是太笨,找了半天,也没改好

如果选中了2级菜单,也可以查询属于它的类

多谢了~
...全文
160 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
wzqing 2009-03-23
  • 打赏
  • 举报
回复
速度好快啊,我试试,先谢谢哈
dawugui 2009-03-23
  • 打赏
  • 举报
回复
create table Area (id int identity,Name varchar(10) ,order_by int ,father_ID int )
insert into area values('广东省',2,0)
insert into area values('四川省',2,0)
insert into area values('湖北省',2,0)
insert into area values('东莞市',1,1)
insert into area values('广州市',1,1)
insert into area values('天河区',0,5)
insert into area values('绵阳市',1,2)
insert into area values('武汉市',1,3)
insert into area values('汉口区',0,8)
insert into area values('随州市',1,3)
create table tmp (id int identity,Name varchar(10) ,order_by int ,father_ID int )
go
--查询指定节点及其所有子节点的函数
create function f_cid(@ID int) returns @t_level table(id int , level int)
as
begin
declare @level int
set @level = 1
insert into @t_level select @id , @level
while @@ROWCOUNT > 0
begin
set @level = @level + 1
insert into @t_level select a.id , @level
from area a , @t_Level b
where a.father_ID = b.id and b.level = @level - 1
end
return
end
go

create proc my_proc
as
begin
declare @id as int
set @id = 0
while exists(select 1 from area where order_by = 2 and id > @id)
begin
set @id = (select min(id) from area where order_by = 2 and id > @id)
insert into tmp(Name ,order_by ,father_ID) select a.name,a.order_by ,a.father_id from area a , f_cid(@id) b where a.id = b.id order by a.id
end
end
go
exec my_proc

select case when order_by = 2 then name
when order_by = 1 then ' ' + name
when order_by = 0 then ' ' + name
end name
from tmp order by id

drop function f_cid
drop proc my_proc
drop table area , tmp

/*
name
--------------
广东省
东莞市
广州市
天河区
四川省
绵阳市
湖北省
武汉市
汉口区
随州市

(所影响的行数为 10 行)

*/
wzqing 2009-03-23
  • 打赏
  • 举报
回复
恩,对
dawugui 2009-03-23
  • 打赏
  • 举报
回复
--你的数据这样才对吧?

create table Area (id int identity,Name varchar(10) ,order_by int ,father_ID int )
insert into area values('广东省',2,0)
insert into area values('四川省',2,0)
insert into area values('湖北省',2,0)
insert into area values('东莞市',1,1)
insert into area values('广州市',1,1)
insert into area values('天河区',0,5)
insert into area values('绵阳市',1,2)
insert into area values('武汉市',1,3)
insert into area values('汉口区',0,8)
insert into area values('随州市',1,3)
go

select * from area

drop table area

/*
id Name order_by father_ID
----------- ---------- ----------- -----------
1 广东省 2 0
2 四川省 2 0
3 湖北省 2 0
4 东莞市 1 1
5 广州市 1 1
6 天河区 0 5
7 绵阳市 1 2
8 武汉市 1 3
9 汉口区 0 8
10 随州市 1 3

(所影响的行数为 10 行)
*/
wzqing 2009-03-23
  • 打赏
  • 举报
回复
好的,多谢·
dawugui 2009-03-23
  • 打赏
  • 举报
回复
好,==.
wzqing 2009-03-23
  • 打赏
  • 举报
回复
能不能帮我写一个啊~~~技术太差了·不会啊
dawugui 2009-03-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wzqing 的回复:]
表Area
字段 自增长ID
Name varchar
order_by int
father_ID int


1,'广东省',2,0
2,'四川省',1,0
3,'湖北省',0,0
4,'东莞市',0,3
5,'广州市',0,3
6,'天河区',0,5
7,'绵阳市',0,2
8,'武汉市',2,3
9,'汉口区',0,8
10,'随州市',1,3


需要的结果就是,按照order_by,ID排序,按FATHER_ID分组显示

四川省
绵阳市
广东省
东莞市
广州市
天河区
湖北省
随州市
武汉市
汉口区
[/Quote]

在存储过程中利用循环逐一条用函数,并把结果插入临时表,最后从临时表取数据.
wzqing 2009-03-23
  • 打赏
  • 举报
回复
表Area
字段 自增长ID
Name varchar
order_by int
father_ID int


1,'广东省',2,0
2,'四川省',1,0
3,'湖北省',0,0
4,'东莞市',0,3
5,'广州市',0,3
6,'天河区',0,5
7,'绵阳市',0,2
8,'武汉市',2,3
9,'汉口区',0,8
10,'随州市',1,3



需要的结果就是,按照order_by,ID排序,按FATHER_ID分组显示

四川省
绵阳市
广东省
东莞市
广州市
天河区
湖北省
随州市
武汉市
汉口区
rucypli 2009-03-23
  • 打赏
  • 举报
回复
with cte test
(
select id,name,order_by,father_id from area where id=@id
union all
select id,name,order_by,father_id from area A where A.father_id=test.id
)
dawugui 2009-03-23
  • 打赏
  • 举报
回复

请给出表结构,测试数据,相关算法和需要的结果.谢谢!

wzqing 2009-03-23
  • 打赏
  • 举报
回复
这个我试过了,总是不对,

我做的这个可能有点问题吧,father_ID,是INT型字段,0是大类,,
dawugui 2009-03-23
  • 打赏
  • 举报
回复
[Quote=引用楼主 wzqing 的帖子:]
表Area
字段
ID,Name,order_by,father_ID

是个无限级的菜单,请帮忙写一个存储过程,
是在是太笨,找了半天,也没改好

如果选中了2级菜单,也可以查询属于它的类

多谢了~
[/Quote]

/*
标题:查询指定节点及其所有子节点的函数
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-05-12
地点:广东深圳
*/

create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
insert into tb values('001' , null , '广东省')
insert into tb values('002' , '001' , '广州市')
insert into tb values('003' , '001' , '深圳市')
insert into tb values('004' , '002' , '天河区')
insert into tb values('005' , '003' , '罗湖区')
insert into tb values('006' , '003' , '福田区')
insert into tb values('007' , '003' , '宝安区')
insert into tb values('008' , '007' , '西乡镇')
insert into tb values('009' , '007' , '龙华镇')
insert into tb values('010' , '007' , '松岗镇')
go

--查询指定节点及其所有子节点的函数
create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int)
as
begin
declare @level int
set @level = 1
insert into @t_level select @id , @level
while @@ROWCOUNT > 0
begin
set @level = @level + 1
insert into @t_level select a.id , @level
from tb a , @t_Level b
where a.pid = b.id and b.level = @level - 1
end
return
end
go

--调用函数查询001(广东省)及其所有子节点
select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
002 001 广州市
003 001 深圳市
004 002 天河区
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇

(所影响的行数为 10 行)
*/

--调用函数查询002(广州市)及其所有子节点
select a.* from tb a , f_cid('002') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
002 001 广州市
004 002 天河区

(所影响的行数为 2 行)
*/

--调用函数查询003(深圳市)及其所有子节点
select a.* from tb a , f_cid('003') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
003 001 深圳市
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇

(所影响的行数为 7 行)
*/

drop table tb
drop function f_cid
jiangshun 2009-03-23
  • 打赏
  • 举报
回复
关注

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧