当在SQL Server中使用CTE递归时,是否有办法显示SQL服务器正在使用的路径ID?SQL服务器递归路径ID
比方说,我有一个代表相互连接的边缘如下表:
CREATE TABLE c(id int, n1 int, n2 int);
insert into c(id, n1, n2) values (100, 1, 2);
insert into c(id, n1, n2) values (101, 2, 3);
insert into c(id, n1, n2) values (102, 3, 4);
insert into c(id, n1, n2) values (103, 4, 8);
insert into c(id, n1, n2) values (104, 3, 11);
insert into c(id, n1, n2) values (105, 11, 12);
我现在可以使用下面的递归查询显示哪些节点相互连接:
WITH Nodes
AS
(
select c.id, c.n1, c.n2, 1 as level from c where id = 100
UNION ALL
select c.id, c.n1, c.n2, level+1 from c inner join Nodes on Nodes.n2 = c.n1 where c.id != 100
)
SELECT id, n1, n2, level from Nodes where level > 1 order by id;
这将得出以下结果:
id n1 n2 level
101 2 3 2
102 3 4 3
103 4 8 4
104 3 11 3
105 11 12 4
有2条路径s,101-102-103 & 101-104-105。我想识别每条路径,但我认为它们实际上与SQL Server正在使用的递归路径相同,所以如果我有一种获取此递归路径ID的方法,那么我可以识别每条唯一路径。
我希望我的输出看起来像这样:
id n1 n2 level path_id
101 2 3 2 1
102 3 4 3 1
103 4 8 4 1
104 3 11 3 2
105 11 12 4 2
正如你所看到的,在节点104的第二路径或递归遇到。这些是构成该路径的节点:
path 1 : 101 - 102 - 103
path 2 : 104 - 105
每个路径实际上开始于节点100,但它是由“电平> 1”子句左出来的结果。
感谢,Steef
结果究竟应该如何相似?你想得到两行结果和每个完整路径吗? –
Hi Georg,我想要一个额外的列'path_id'追加 – Steef
行,但是,然后,你需要输出ID为“101”TWICE,一次path_id“1”,一次path_id“2” –