2014-03-24

create number tables for months and days - part 2 #SQLServer

As mentioned in our last post, here's a better way to create a table of numbers.

;with cte as (
select 1 as n union all select n+1 from cte where n+1 <= 31
) select n from cte

This query uses a recursive Common Table Expression.

More information abount Recursive CTE's can be found here:
It might be a little bit confuse the first time you use it, but remember that a Recursion is a the result of stacked statements. In this particular case, we are stacking queries.

Any doubts and questions, fill in the comment section bellow.

No comments:

Post a Comment