The following function can be created to dyanmically create a number list in SQL:
CREATE FUNCTION GetNumberList ( @Start INt ,@MaxNum Int ) RETURNS @ret TABLE ( Number INT PRIMARY KEY ) AS BEGIN -- Error if over 131071 due to performance issues (look at creating a permanent numbers table) IF @MaxNum > 131071 INSERT INTO @ret VALUES(CAST('GetNumberList only works up to 131071' AS INT)) -- Work out how many recursions to do DECLARE @Recurse INT = 0; IF(@MaxNum > 0) SET @Recurse = FLOOR(LOG(@MaxNum,2)) -- Insert the first 2 values INSERT INTO @ret VALUES(@Start),(@Start+1) WHILE @Recurse > 0 BEGIN INSERT @ret SELECT Number + (SELECT COUNT(Number) FROM @ret) FROM @ret SET @Recurse -= 1 END DELETE FROM @ret WHERE Number > @Start + @MaxNum RETURN END GO