Skip to content

Commit 65abacb

Browse files
committed
Update Patterns Recursive CTEs.md
1 parent 9fc6f5a commit 65abacb

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

Patterns Recursive CTEs.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ parent: Design Patterns
66
permalink: /patterns/rec-cte
77
---
88

9+
Because, by design, SQL delegates the flow control of statement evaluation to the database engine, the standard SQL grammar does not include any flow control structures. The only exception is expression-level branching control. This control structure has operator and function representation, which is why it naturally integrates within an expression. Adding statement-level flow controls, such as branching or loops, necessitates grammar extension or special conventions. Recursive CTEs (RCTEs) follow the latter approach and implement the while/repeat loop structure. A general loop structure has four essential components: initialization code, the body of the loop, syntactic elements marking the beginning and end of the body code, and a means to exit the loop (termination conditions).
10+
11+
RCTEs do not introduce any special grammar/syntax; instead, it uses the standard SELECT statement syntax and a special convention. The result is quite contrived; for this reason, comprehending and mastering RCTEs may not be straightforward. Because the sole function of SQL is the construction and manipulation of row sets (views), the initialization and loop body blocks of an RCTE constitute complete SELECT statements. The initialization query generates starting view used as one of the sources by the body query. Each execution cycle of the loop body yields a new view used as one of the sources by the following loop cycle. There are two possible termination conditions. If the loop body includes the LIMIT clause, the loop terminates after the specified number of rows is processed. Alternatively, the loop terminates after processing all previously returned rows, with the loop body returning empty row sets.
12+
13+
By convention, initialization and loop body SELECT blocks should be part of the same CTE. Because a CTE is, in turn, a SELECT statement, a set operator (such as UNION) must glue the initialization and loop body blocks, yielding a compound SELECT. Naturally, the initialization block goes first, followed by the body. The body query, by convention, uses its CTE name as one of the sources in the FROM clause. The CTE may include multiple set operations, and the first SELECT block referencing its CTE's name constitutes marks the beginning of the body block, with all preceding code forming the initialization query.
14+
15+
The resulting view of an RCTE query is the compound consisting of the row set produced by the initialization query combined with all row sets returned by individual loop cycles using the specified set operator (the operator separating the initialization and the body blocks).
16+
17+
Yet another convention is related to the meaning of the RCTE name. Outside the RCTE, its name refers to the resulting view above. This meaning is the same for both ordinary and recursive CTEs. An ordinary CTE cannot use its name as one of its sources. Within an RCTE, on the other hand, its name refers to the processing frame/view, which, to a first approximation, contains the row set returned by the previous cycle or the initialization query for the first cycle. More precisely, the processing frame is a single row view (in a sense, a pointer to the next exiting row of the processing queue) sitting at the tip of a dynamically generated processing queue, as discussed later.
18+
919
[Recursive CTEs][] is
1020

1121
~~~sql

0 commit comments

Comments
 (0)