World's Simplest Recursive CTE?
While preparing for a SQL PASS a year or two ago when I was doing a session about common table expressions, I had endeavored to come up with an extremely simple recursive CTE. Unfortunately, I wasn’t creative enough at the time to come up with anything, so the attendees had to sit through more practical examples.
But later I revisited the problem, and came up with this:
WITH SimpleCTE(Number) AS
(
SELECT 1
UNION ALL
SELECT * FROM SimpleCTE WHERE 0=1
)
SELECT * FROM SimpleCTE
No guarantees that I couldn’t get it even simpler, but this is the simplest I've ever seen. There may be another option for the SELECT * part of the CTE, but I’m not seeing it right now.
BUT! Obviously, this is the result of a thought experiment, and is not practical for anything else I can think of. Don’t take this as any kind of recommended practice!