急!求数表的访问存储过程

zzunicholas 2010-10-04 08:42:25

ID(GUID) Parent(GUID) Name

生成某ID下的深度排序
...全文
100 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xman_78tom 2010-10-04
  • 打赏
  • 举报
回复

declare @id uniqueidentifier;
set @id = '00000000-0000-0000-0000-000000000000';

;with t as (
select id,pid, name, 0 level from tb where id=@id
union all
select tb.id, tb.pid, tb.name, t.level+1 from t, tb where tb.pid=t.id
)
select id, pid, name from t order by level;
zzunicholas 2010-10-04
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 xman_78tom 的回复:]

SQL code

declare @id uniqueidentifier;
set @id = ...

;with t as (
select id,pid, name, 0 level from tb where id=@id
union all
select tb.id, tb.pid, tb.name, t.level+1 from t, tb where tb.pid=t.……
[/Quote]

前辈您好,如果我的表的根节点的parent的Guid.Empty表示的,
该如何查询整个树呢?

非常感谢!
xman_78tom 2010-10-04
  • 打赏
  • 举报
回复

declare @id uniqueidentifier;
set @id = ...

;with t as (
select id,pid, name, 0 level from tb where id=@id
union all
select tb.id, tb.pid, tb.name, t.level+1 from t, tb where tb.pid=t.id
)
select id, pid, name from t order by level;
zzunicholas 2010-10-04
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hao1hao2hao3 的回复:]

深度排序??递归吗?

try

http://tech.e800.com.cn/articles/2010/16/1262758981700_1.html
[/Quote]

CREATE PROCEDURE dbo.GetNodeTree
(
@node uniqueidentifier
)
AS
SET NOCOUNT ON
WITH cte (ID,Parent,Name,Part) as
(
SELECT ID, Parent, Name ,Part
FROM PartsClass a
WHERE a.Parent=node
UNION ALL
SELECT cte.ID, cte.Parent, cte.Name,cte.Part
FROM cte
JOIN PartsClass e
ON cte.Parent = e.ID
)
--Uses MAXRECURSION to limit the recursive levels to 2
SELECT *
FROM cte
--OPTION (MAXRECURSION 2);
RETURN

我照着改的,但是保存的时候说存储不完整

请问我哪里有错吗?第一次用,模仿者用的
hao1hao2hao3 2010-10-04
  • 打赏
  • 举报
回复

34,837

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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