SQL Server CTE and recursion example

后端 未结 4 1010
臣服心动
臣服心动 2020-11-22 07:03

I never use CTE with recursion. I was just reading an article on it. This article shows employee info with the help of Sql server CTE and recursion. It is basically showing

4条回答
  •  无人及你
    2020-11-22 07:33

    The execution process is really confusing with recursive CTE, I found the best answer at https://technet.microsoft.com/en-us/library/ms186243(v=sql.105).aspx and the abstract of the CTE execution process is as below.

    The semantics of the recursive execution is as follows:

    1. Split the CTE expression into anchor and recursive members.
    2. Run the anchor member(s) creating the first invocation or base result set (T0).
    3. Run the recursive member(s) with Ti as an input and Ti+1 as an output.
    4. Repeat step 3 until an empty set is returned.
    5. Return the result set. This is a UNION ALL of T0 to Tn.

提交回复
热议问题