請參閱下面的DDL:遞歸SQL查詢來識別
CREATE TABLE #MasterChild (MasterID decimal(25,0), ChildID decimal(25,0), ChildCreTime datetime)
insert into #MasterChild values (150021032000000173536533,150021032000000173946207,'2016-04-22 13:27:30.483')
insert into #MasterChild values (150021032000000173571072,150021032000000173946207,'2016-04-22 13:27:30.483')
insert into #MasterChild values (150021032000000173573651,150021032000000173946207,'2016-04-22 13:27:30.483')
insert into #MasterChild values (150021032000000173574917,150021032000000173946207,'2016-04-22 13:27:30.483')
insert into #MasterChild values (150021032000000173582487,150021032000000173946207,'2016-04-22 13:27:30.483')
insert into #MasterChild values (150021032000000173604342,150021032000000173946207,'2016-04-22 13:27:30.483')
insert into #MasterChild values (150021032000000173931636,150021032000000173946207,'2016-04-22 13:27:30.483')
insert into #MasterChild values (150021032000000173935066,150021032000000173946207,'2016-04-22 13:27:30.483')
insert into #MasterChild values (150021032000000173946207,150021032000000173952172,'2016-04-22 13:38:32.747')
insert into #MasterChild values (150021032000000173946207,150021032000000173954415,'2016-04-22 13:43:28.120')
insert into #MasterChild values (150021032000000173536533,150021032000000173954321,'2016-04-22 13:43:28.120')
insert into #MasterChild values (150021032000000173954321,150021032000000173954319,'2016-04-22 13:43:28.120')
和下面的SQL:
with GetAllMerges
as
(
select masterid,childid from #MasterChild where masterid=150021032000000173571072 and childid=150021032000000173946207
union all
select #MasterChild.masterid,#MasterChild.childid from #MasterChild inner join GetAllMerges
on
#MasterChild.childid=GetAllMerges.masterid
or (#MasterChild.childid=GetAllMerges.childid and #MasterChild.masterid<>GetAllMerges.masterid)
)
select distinct masterid,childid from GetAllMerges
我看到下面的錯誤:
Msg 530, Level 16, State 1, Line 1
The statement terminated. The maximum recursion 100 has been exhausted before statement completion.
這是爲什麼?我試圖展示所有關聯的孩子和主人。例如,如果主人1連接到孩子2,並且孩子2連接到主人3,並且主人3連接到孩子4並且孩子4連接到主人5,則連接人物1-5(所有孩子和主人都是人)。
在大多數情況下 - 當遞歸CTE達到100的默認值,因此打破它不是一個'MAXRECURSION'的問題,而是一個錯誤遍歷代碼的問題:-)另一個常見問題是,如果最後一個孩子與t有關他的父母以CTE在圈子裏跑的方式...... – Shnugo