一个树形结构的存储过程 不不知道为什么只能读取 父节点为0的

liyj19870228 2010-06-11 10:40:17
一个树形结构的存储过程 不不知道为什么只能读取 父节点ID为0的

读取不了次一级别的id

第一次选 CategoryId=3 得出结果

3 根节点1 1 测试meta 测试key 测试Description 0 1 1 NULL 1 0 0 0
4 子节点AB 2 测试metaab 测试keyab 测试Description 3 1 1 NULL 1 0 1 00000001
8 子节点121 NULL 子节点121 子节点121 NULL 4 NULL NULL NULL 1 0 2 00000001
9 子节点1212 NULL 子节点1212 子节点1212 子节点1212 8 NULL NULL NULL 1 0 3 00000001
7 子节点13 NULL 子节点13 子节点13 NULL 3 NULL NULL NULL 1 0 1 00000002

第二次 选CategoryId=4 得出结果

就没有结果了。。
我不知道哪里除了问题 我贴出代码 麻烦各位大哥大姐帮我修改写。。


相关的聚合函数如下:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER FUNCTION [dbo].[Lpad]
(
@i int,@len int
)
RETURNS nvarchar(max)
AS
BEGIN
RETURN cast (replicate('0', @len - len(@i) ) + convert(nvarchar,@i) as nvarchar(max))
END



对应的sql语句

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[LanYu_Categories_GetList]
@CategoryId int
AS
BEGIN
with RelClass
as
(
select *,0 as Level,cast('0' as nvarchar(max)) as treepath
from LanYu_Categories where CategoryId = @CategoryId and ParentCategoryId=0
union all
select csc.*,
rc.[Level] + 1,dbo.Lpad(Row_Number()
over (order by csc.OrderID desc),8) as treepath
from LanYu_Categories as csc inner join RelClass as rc on
csc.ParentCategoryId = rc.CategoryId )
SELECT * from RelClass order by treepath
END


麻烦大家了。。
...全文
89 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
rmljoe 2010-06-11
  • 打赏
  • 举报
回复
你的存储过程里有这句条件啊!


set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[LanYu_Categories_GetList]
@CategoryId int
AS
BEGIN
with RelClass
as
(
select *,0 as Level,cast('0' as nvarchar(max)) as treepath
from LanYu_Categories where CategoryId = @CategoryId and ParentCategoryId=0
union all
select csc.*,
rc.[Level] + 1,dbo.Lpad(Row_Number()
over (order by csc.OrderID desc),8) as treepath
from LanYu_Categories as csc inner join RelClass as rc on
csc.ParentCategoryId = rc.CategoryId )
SELECT * from RelClass order by treepath
END
ChinaJiaBing 2010-06-11
  • 打赏
  • 举报
回复

--try

if object_id ('tb')is not null
drop table tb
go
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
if object_id('c_f') is not null
drop function c_f
go
create function c_f
(@pid int)
returns @table table (pid int,level int)
as
begin
declare @level int
set @level=1
insert into @table select @pid,@level
while @@rowcount>0
begin
set @level=@level+1
insert into @table
select a.id,@level from tb a join @table b
on a.pid=b.pid and b.level=@level-1
end
return
end
go
select a.* from tb a,c_f(002) b
where a.id=b.pid


/*


id pid name
---- ---- ----------
002 001 广州市
004 002 天河区

(2 行受影响)




*/

ChinaJiaBing 2010-06-11
  • 打赏
  • 举报
回复

---try

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



liyj19870228 2010-06-11
  • 打赏
  • 举报
回复

SELECT [CategoryId]
,[Name]
,[DisplaySequence]
,[Meta_Description]
,[Meta_Keywords]
,[Description]
,[ParentCategoryId]
,[Depth]
,[Path]
,[RewriteName]
,[TypeId]
,[OrderID]

这个是数据库结构

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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