r/SQL • u/SilEventide • Sep 04 '26
SQL Server How does a Recursive CTE work exactly?
With RecursiveEven20 As
(
Select 0 As Numbers,
0 As RunningCount
Union All
Select Numbers + 2,
Count(RunningCount) Over() As RunningCount
From RecursiveEven20
Where RunningCount < 19
)
Select *
From RecursiveEven20;From how much I know about recursive CTE, I thought this would work, Initially I felt I was doing a semantic error, then when I tried to see where the fault is, I realised Count isn't incrementing at all, its as if only the last feedback row is available to it. I tried using explicit frame window, same result. I think I dont understand exactly how recursie CTE works, I tried AI, its explanation is bit difficult to understand.
I am a beginner by the way, learned these recently so I wanted to mix them all up.
20
Upvotes
3
u/GTS_84 Sep 04 '26
The main issue I see with this is that it's never going to stop. not with that window function like that. It will hit the max recursions.
try this:
25
u/cush2push Sep 04 '26
Story with a query example.
Leo wanted to find every toy hidden inside his stack of mystery boxes, so he wrote a special two-step rule on his magical chalkboard. First, his starting rule told his robot helper to open the big red box right on the top shelf and drop the toy inside into a wagon. Next, his loop rule told the robot to look inside whatever box it just opened, grab any smaller boxes hiding inside, and repeat the trick over and over. The robot read the chalkboard and kept running back and forth, opening boxes inside of boxes and dumping all the toys into the wagon until it found its final box. When there are no more boxes left to open, the robot stops running and rolls the wagon right back to Leo.
IMO I love CTEs. i have been able to reduce the run time of some very long queries by hours because of CTEs and using them for repeating information grabs
1
u/blackleather90 Sep 04 '26
Recursive CTEs are a very easy way to find hierarchies.
For example a line manager chain.
If you do the normal left join to find a manager of a use, then you need another left join to find its manager and so on.
Ah and after you have the circular references 😅
I have a link that I use every time I need a refresher. Just not at hand. Let me know if you still need it
1
u/DamienTheUnbeliever Sep 04 '26
Any time you try to impose imperative thinking on a query, you're doing things wrong. SQL queries are *declarative* and you should say *what you want*,not *how to do it*. Unfortunately you've just stamped imperative thinking all over this attempt so it's difficult to provide advice on how to fix this.
2
u/xeroskiller Solution Architect Sep 04 '26
They loop. Run the base step, count resultset, run recursive step, count resultset, if unchanged return, otherwise repeat recursive step and resultset count check. Fail beyond a certain loop depth.
1
u/datadriven_io Sep 04 '26
With the addition of recursive CTEs and window functions, SQL is actually Turing-complete. If you want to solidify this, cte practice on datadriven.io is worth doing. Meaning that you could, for example, write a compiler in pure SQL, although you very likely would not enjoy the experience.
1
u/billbot77 Sep 06 '26
The trick to these is to remember that it is not iterating over a variable, it's progressively adding rows to a table in memory and adding another row based on the whole table up to that point. I.e. the table, IS the variable.
1
u/therealdrsql Sep 06 '26
What do you mean that it doesn’t work? Syntax? Wrong answer? Just wondering, never tried a COUNT or a window function in a recursive CTE so just wondered what you were trying.