Home Forums SQL Server 2008 SQL Server Newbies Work with CTE when data itself has discrepency(infinite loop. RE: Work with CTE when data itself has discrepency(infinite loop.

  • It's actually a leftover from when I had to process the whole table at once. It's taking the GroupID value from the parent node and comparing it each child to prevent cyclic references. As you're using a single group, the query would have only needed an additional condition.

    WITH CTE( groupID, objectType, memberID, [Nodelevel]) as

    (

    SELECT

    tm.groupID

    , tm.objectType

    , tm.memberID

    , 0 as [Nodelevel]

    from #Hierarchy tm with(nolock) -- drop table #tblGrpsMatc

    where groupID = 8

    UNION ALL

    SELECT

    TC.groupID

    ,TC.objectType

    , TC.memberID

    , [Nodelevel] + 1 as [Nodelevel]

    FROM CTE C2

    inner join #Hierarchy TC with(nolock)

    ON tc.groupID = C2.memberID

    AND TC.groupID <> TC.memberID

    WHERE TC.groupID <> 8

    )

    SELECT groupID, objectType, memberID, [Nodelevel]

    FROM CTE;

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2