Recursive CTE Concept Confusion

后端 未结 3 1011
清酒与你
清酒与你 2021-01-25 11:50

I am trying to understand the concepts of using CTE in my SQL code. I have gone through a number of online posts explaining the concept but I cannot grasp how it iterates to pre

3条回答
  •  没有蜡笔的小新
    2021-01-25 12:15

    It's all about recursive step: firstly, root is used to proceed first step of recursion, so:

    SELECT EmployeeID, ContactID, LoginID, ManagerID, Title, BirthDate
    FROM HumanResources.Employee
    WHERE ManagerID IS NULL
    

    This provides first set of records.

    Second set of records will be queried based on first set (anchor), so it will query all employees, which have manager in first set.

    Second step of recursion will be based on second result set, not the anchor.

    Third step will be based on third result set, etc.

提交回复
热议问题