Not logged in - Login

Use recursive query to output a select list with a range of values

Use the following example to output a select list with a range of values.

declare @minYear int
   , @maxYear int

select @minYear = 2008
   , @maxYear = 2018

;with cteYears as (
   -- this is the starting value (for the first row)
   select Year1 = @minYear
   union all
   -- this is the calculation for subsequent values (for the other rows).
   select Year1 = a.Year1 + 1
   from cteYears a
   -- the 'where' clause specifies the exit condition, or when to stop.
   where a.Year1 < @maxYear
)
select y.*
from cteYears y
option (maxrecursion 10000)