Recursive CTEs in SQL Server execute involving a temp table. This is probably why this tiny amount of work even takes a measurable amount of time. This should take <= 1ms.
Use a numbers table. Or, use one of the many tricks to materialize a sequence of numbers without table access such as:
SELECT Num
FROM (VALUES (1), (2), ...) x(Num)
Using Number % 15 = 0
instead of Number % 3 = 0 AND Number % 5 = 0
is less clear. It requires mathematical insight to convince anyone that this is even correct. Code should be obviously correct.
The default length of varchar
is unclear. (Can you tell from memory?!) Better use nvarchar(400)
. By default, use Unicode characters in order to just never have certain bugs and problems.
These issues aside this query is simple. Anyone can very quickly understand it and conclude that it is correct. That's good code.
WITH RECURSIVE
(I believe this is standard), but SQL Server doesn't seem to like theRECURSIVE
keyword. SQLite 3 doesn't care either way. Oracle has its own weird syntax involvingSTART
andCONNECT
keywords. MySQL (still) doesn't support CTEs. \$\endgroup\$