Tuesday, April 16, 2013

MS SQL CTE wonders Display 1 to 100 without Looping

I happened to solve a puzzle.
Display 1 to 100 in SSMS without using any loop or table or table variable or even cursor.

I gave up, then got the solution from requester.


WITH cte 
     AS (SELECT 1 Num          UNION ALL          SELECT num + 1          FROM   cte          WHERE  num < 100) SELECT * FROM   cte 


Always wonder with common table expressions. happy querying