[英]Hierarchical data run infinitely in SQL Server 2008
我正在使用SQL Server 2008.在我的数据库中,一个表如下:
------------------------------
id parentId name
------------------------------
1 NULL india
2 1 gujrat
3 1 Maharastra
4 1 rajsthan
5 2 ahmedabad
6 2 rajkot
7 NULL USA
8 7 newyork
1 3 mumbai
1 3 goa
1 4 jaipur
1 7 californiya
在这里,我想用层次结构中的级别获取此数据,因此我创建了如下查询:
with RecursiveTable_CTE (Id,Parentid,Name_cte,Hlevel)
as
(
select id , parentId, name, 0 as Hlevel from treeTable where id = 1
union all
select t.id,t.parentId,t.name, Hlevel + 1 as LEVEL from treeTable as t inner join RecursiveTable_CTE as rtc on t.id = rtc.Parentid
)
select * from RecursiveTable_CTE option (maxrecursion 0)
并尝试
with RecursiveTable_CTE (Id,Parentid,Name_cte,Hlevel)
as
(
select id , parentId, name, 0 as Hlevel from treeTable where parentId is null
union all
select t.id,t.parentId,t.name, Hlevel + 1 as LEVEL from treeTable as t inner join RecursiveTable_CTE as rtc on rtc.id = t.parentId
)
select * from RecursiveTable_CTE option (maxrecursion 0)
但都导致无限循环。
谁能帮我?
我认为你追求的问题是这样的:
with RecursiveTable_CTE (Id,Parentid,Name_cte,Hlevel)
as
(
select id , parentId, name, 0 as Hlevel
from treeTable
where parentId is null
union all
select t.id,t.parentId,t.name, rtc.Hlevel + 1 as Hlevel
from treeTable as t
inner join RecursiveTable_CTE as rtc on t.Parentid = rtc.id
)
select * from RecursiveTable_CTE
问题是,我认为您的数据不正确,并且id
和parentId
在某些行中交换。 我用过这个数据:
insert into treeTable values (1, NULL, 'india')
insert into treeTable values (2, 1, 'gujrat')
insert into treeTable values (3, 1, 'Maharastra')
insert into treeTable values (4, 1, 'rajsthan')
insert into treeTable values (5, 2, 'ahmedabad')
insert into treeTable values (6, 2, 'rajkot')
insert into treeTable values (7, NULL, 'USA')
insert into treeTable values (8, 7 , 'newyork')
insert into treeTable values (9, 1 , 'mumbai')
insert into treeTable values (10, 1 , 'goa')
insert into treeTable values (11, 1 , 'jaipur')
insert into treeTable values (12, 7 , 'californiya')
在你的情况下,1是顶级,但1又有父3,7,7。
你的无限循环是由于你错误的层次结构。
id parentId name
------------------------------
1 NULL india
1 3 mumbai
1 4 jaipur
1 7 californiya
您在ID列中有重复项,这是一个错字吗? 如果你没有重复
CREATE TABLE states
(
[id] INT,[parentid] INT,[name] VARCHAR(11)
);
INSERT INTO states
([id],[parentid],[name])
VALUES (1,NULL,'india'),
(2,1,'gujrat'),
(3,1,'Maharastra'),
(4,1,'rajsthan'),
(5,2,'ahmedabad'),
(6,2,'rajkot'),
(7,NULL,'USA'),
(8,7,'newyork'),
(9,3,'mumbai'),
(10,3,'goa'),
(11,4,'jaipur'),
(12,7,'californiya');
WITH recursivetable_cte (id, parentid, name_cte, hlevel)
AS (SELECT id,parentid,name,0 AS Hlevel
FROM states
WHERE parentid IS NULL
UNION ALL
SELECT t.id,t.parentid,t.name,hlevel + 1 AS LEVEL
FROM states AS t
INNER JOIN recursivetable_cte AS rtc
ON t.parentid = rtc.id)
SELECT *
FROM recursivetable_cte
OPTION (maxrecursion 0)
@Szymom是对的.id应该是唯一的。虽然在这个查询中不需要更改表数据
看到变化,
;with RecursiveTable_CTE (Id,Parentid,Name_cte,Hlevel)
as
(
select id , parentId, name, 0 as Hlevel from @t where id = 1 **and parentId is null**
union all
select t.id,t.parentId,t.name, Hlevel+ 1 as LEVEL from @t as t
inner join RecursiveTable_CTE as rtc on t.Parentid = rtc.id **and t.id<>1**
)
select * from RecursiveTable_CTE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.