How does a Recursive CTE run, line by line?

后端 未结 5 1984
情书的邮戳
情书的邮戳 2020-11-27 12:33

I think I\'ve got the format of Recursive CTEs down well enough to write one, but still find myself frustrated to no end that I cannot manually process one (pretend to be th

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-27 13:15

    I think it breaks down like this:

    1. The anchor statement is executed. This gives you a set of results, called the base set, or T0.

    2. The recursive statement is executed, using T0 as the table to execute the query against. This happens automatically when you query a CTE.

    3. If the recursive member returns some results, it creates a new set, T1. The recursive member is then executed again, using T1 as input, creating T2 if there are any results.

    4. Step 3 continues until no more results are generated, OR the maximum number of recursions has been met, as set by the MAX_RECURSION option.

    This page probably explains it best. It has a step-by-step walkthrough of the execution path of a CTE.

提交回复
热议问题