带OPTION的无限循环CTE(maxrecursion 0)

时间:2013-02-26 03:35:38

标签: sql-server sql-server-2008 common-table-expression

我有CTE查询,上面有大量记录。以前它工作得很好。但最近,它为一些成员抛出错误

  

声明终止。在语句完成之前,最大递归100已经用尽。

所以我将OPTION (maxrecursion 0)OPTION (maxrecursion 32767)放在我的查询上,因为我不想限制记录。但是,结果是查询需要永远加载。我该如何解决这个问题?

这是我的代码:

with cte as(
-- Anchor member definition
    SELECT  e.SponsorMemberID , e.MemberID, 1 AS Level
    FROM tblMember AS e 
    where e.memberid = @MemberID

union all

-- Recursive member definition
    select child.SponsorMemberID , child.MemberID, Level + 1
    from tblMember child 

join cte parent

on parent.MemberID = child.SponsorMemberID
)
-- Select the CTE result
    Select distinct a.* 
    from cte a
    option (maxrecursion 0)

编辑:删除不必要的代码以便于理解

已解决:因此问题不是来自maxrecursion。它来自CTE。我不知道为什么,但可能包含任何赞助商周期:A - > B - > C - > A - > ......(感谢@HABO)

我尝试了这种方法并且有效。 Infinite loop in CTE when parsing self-referencing table

1 个答案:

答案 0 :(得分:9)

如果您达到递归限制,您要么在赞助关系或数据循环方面有相当深度。像下面这样的查询将检测循环并终止递归:

declare @tblMember as Table ( MemberId Int, SponsorMemberId Int );
insert into @tblMember ( MemberId, SponsorMemberId ) values
  ( 1, 2 ), ( 2, 3 ), ( 3, 5 ), ( 4, 5 ), ( 5, 1 ), ( 3, 3 );
declare @MemberId as Int = 3;
declare @False as Bit = 0, @True as Bit = 1;

with Children as (
  select MemberId, SponsorMemberId,
    Convert( VarChar(4096), '>' + Convert( VarChar(10), MemberId ) + '>' ) as Path, @False as Loop
    from @tblMember
    where MemberId = @MemberId
  union all
  select Child.MemberId, Child.SponsorMemberId,
    Convert( VarChar(4096), Path + Convert( VarChar(10), Child.MemberId ) + '>' ),
    case when CharIndex( '>' + Convert( VarChar(10), Child.MemberId ) + '>', Path ) = 0 then @False else @True end
    from @tblMember as Child inner join
      Children as Parent on Parent.MemberId = Child.SponsorMemberId
    where Parent.Loop = 0 )
  select *
    from Children
    option ( MaxRecursion 0 );