recursion-使用递归获取.Net中父文件夹的序列

发布于 2024-12-10 13:40:52 字数 861 浏览 0 评论 0原文

我有一个具有以下结构的表

    ID          Name                                               Parent
----------- -------------------------------------------------- -----------
1           Root                                               NULL
2           Root_A                                             1
3           Root_B                                             1
4           Root_C                                             1
5           Root_C_A                                           4
6           Root_C_A_A                                         5
7           Root_C_A_A_A                                       6

因此,如果我传递7,我想得到以下结果

Root --> Root_C --> Root_C_A --> Root_C_A_A

这意味着我想从子项遍历回根。如何使用 SQL Server 2008 存储过程或其他 .Net 技术来完成此操作?

我想我可以使用递归函数完成任务

I have a table with the following structure

    ID          Name                                               Parent
----------- -------------------------------------------------- -----------
1           Root                                               NULL
2           Root_A                                             1
3           Root_B                                             1
4           Root_C                                             1
5           Root_C_A                                           4
6           Root_C_A_A                                         5
7           Root_C_A_A_A                                       6

So if I pass 7, I would like to get the following

Root --> Root_C --> Root_C_A --> Root_C_A_A

That means I want to traverse back to root from a child. How can I do it using SQL Server 2008 Stored Procedures or with other .Net techniques ?

I think I can accomplish the task using recursive function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

中性美 2024-12-17 13:40:52

您可以使用公用表表达式在 Sql Server 2005+ 中实现递归。 CTE 允许您加入自身以进行递归。 CTE 继续递归,直到没有行返回,因此您需要确保可靠地满足该结束条件。

declare @folders as table (id int, name nvarchar(20), parent int);
insert into @folders values(1, 'Root',  null);
insert into @folders values(2, 'Root_A', 1);
insert into @folders values(3, 'Root_B', 1);
insert into @folders values(4, 'Root_C', 1);
insert into @folders values(5, 'Root_C_A', 4);
insert into @folders values(6, 'Root_C_A_A', 5);
insert into @folders values(7, 'Root_C_A_A_A', 6);

declare @folderID int;
set @folderID=7;

with Folders (id, name, parent, number) as
(
    select ID, name, parent, 0 as number 
        from @folders 
        where id=@folderID
    union all
    select i.ID, i.Name, i.Parent, d.number + 1
        from @folders as i
        inner join Folders as d on d.Parent = i.ID
)
select id, name, number
from Folders
order by number desc;

You can implement recursion in Sql Server 2005+ using a common table expression. CTEs let you join to themselves in order to recurse. The CTE continues to recurse until no rows are returned, so you'll want to ensure you reliably meet that end condition.

declare @folders as table (id int, name nvarchar(20), parent int);
insert into @folders values(1, 'Root',  null);
insert into @folders values(2, 'Root_A', 1);
insert into @folders values(3, 'Root_B', 1);
insert into @folders values(4, 'Root_C', 1);
insert into @folders values(5, 'Root_C_A', 4);
insert into @folders values(6, 'Root_C_A_A', 5);
insert into @folders values(7, 'Root_C_A_A_A', 6);

declare @folderID int;
set @folderID=7;

with Folders (id, name, parent, number) as
(
    select ID, name, parent, 0 as number 
        from @folders 
        where id=@folderID
    union all
    select i.ID, i.Name, i.Parent, d.number + 1
        from @folders as i
        inner join Folders as d on d.Parent = i.ID
)
select id, name, number
from Folders
order by number desc;
末蓝 2024-12-17 13:40:52

试试这个:

declare @result varchar(100)
declare @id int
set @id = 7
select @result=Name,@id=Parent from temp where id=@id
while(exists(select 1 from temp where id=@id))
begin
    select @id=Parent,@result=case @result when '' then Name else Name + '-->' end + @result from temp where id=@id
end
select @result

try this:

declare @result varchar(100)
declare @id int
set @id = 7
select @result=Name,@id=Parent from temp where id=@id
while(exists(select 1 from temp where id=@id))
begin
    select @id=Parent,@result=case @result when '' then Name else Name + '-->' end + @result from temp where id=@id
end
select @result
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文