Following query will return 1-10 in 10 rows.
DECLARE @Range AS INT = 10
;WITH CTE AS(
SELECT TOP (@Range) Duration = ROW_NUMBER() OVER(ORDER BY OBJECT_ID)
FROM sys.all_columns
ORDER BY [Object_id]
)
SELECT Duration from CTE
But when I set @Range as 10000 it returns 7374 rows. Why this query can't return more than 7374 rows.
UPDATE
I just found another way to achieve my requirement as following
DECLARE @start INT = 1;
DECLARE @end INT = 10;
WITH numbers AS (
SELECT @start AS number
UNION ALL
SELECT number + 1
FROM numbers
WHERE number < @end
)
SELECT *
FROM numbers
OPTION (MAXRECURSION 0);
Without last line of code it breaks with error Maximum recursion 100 has been exhausted before statement completion and I found this line is specifying 0 for infinite recursion. But this query seems a little slower to me. Is there any faster way???
As commented earlier, it's because you reached the number of rows of sys.columns. Here is another way to generate list of numbers or what others call Numbers Table or Tally Table.
This uses cascaded CTEs and is said to be the fastest way to create a Tally Table:
DECLARE @Range AS INT = 7374
;WITH E1(N) AS( -- 10 ^ 1 = 10 rows
SELECT 1 FROM(VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1))t(N)
),
E2(N) AS(SELECT 1 FROM E1 a CROSS JOIN E1 b), -- 10 ^ 2 = 100 rows
E4(N) AS(SELECT 1 FROM E2 a CROSS JOIN E2 b), -- 10 ^ 4 = 10,000 rows
E8(N) AS(SELECT 1 FROM E4 a CROSS JOIN E4 b), -- 10 ^ 8 = 10,000,000 rows
CteTally(N) AS(
SELECT TOP(@Range) ROW_NUMBER() OVER(ORDER BY(SELECT NULL))
FROM E8
)
SELECT * FROM CteTally
You could easily add another CTE if you need more than 10,000 rows.
For more information about Tally Table, read this excellent article by Jeff Moden.
For performance comparisons among ways to generate Tally Tables, read this.
Explanation taken from Jeff's article:
The CTE called
E1(as in 10E1 for scientific notation) is nothing more than tenSELECT 1's returned as a single result set.
E2does aCROSS JOINofE1with itself. That returns a single result set of 10*10 or up to 100 rows. I say "up to" because if the TOP function is 100 or less, the CTE's are "smart" enough to know that it doesn't actually need to go any further andE4andE8won't even come into play. If theTOPhas a value of less than 100, not all 100 rows thatE2is capable of making will be made. It'll always make just enough according to theTOPfunction.You can follow from there.
E4is aCROSS JOINofE2and will make up to 100*100 or 10,000 rows andE8is aCROSS JOINofE4which will make more rows than most people will ever need. If you do need more, then just add anE16as aCROSS JOINofE8and change the finalFROMclause toFROM E16.What's really amazing about this bad-boy is that is produces ZERO READS. Absolutely none, nada, nil.
One way to generate a large series of numbers would be to use a cross join to create a cartesian product between two tables which will generate a set that is n^2 in size.
This approach however performs a lot worse than the solution put forward in the answer by Felix Pamittan and therefore shouldn't be used.
DECLARE @Range AS INT = 10000
;WITH CTE AS(
SELECT TOP (@Range) Duration = ROW_NUMBER() OVER(ORDER BY (SELECT NULL))
FROM sys.all_columns a CROSS JOIN sys.all_columns b
)
SELECT Duration from CTE
This would generate a set of 54375876 rows in your case. Instead of generating the rows on the fly you should consider creating a tally table suitable for your needs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With