I have two numbers as input from the user, like for example 1000
and 1050
.
How do I generate the numbers between these two numbers, using a sql query, in seperate rows? I want this:
1000
1001
1002
1003
.
.
1050
I have two numbers as input from the user, like for example 1000
and 1050
.
How do I generate the numbers between these two numbers, using a sql query, in seperate rows? I want this:
1000
1001
1002
1003
.
.
1050
This will also do
2 years later, but I found I had the same issue. Here is how I solved it. (edited to include parameters)
slartidan's answer can be improved, performance wise, by eliminating all references to the cartesian product and using
ROW_NUMBER()
instead (execution plan compared):Wrap it inside a CTE and add a where clause to select desired numbers:
I had to insert picture filepath into database using similar method. The query below worked fine:
The code for you would be:
declare @start int = 1000 declare @end int =1050
;with numcte
AS
(
SELECT @start [SEQUENCE]
UNION all
SELECT [SEQUENCE] + 1 FROM numcte WHERE [SEQUENCE] < @end )
SELECT * FROM numcte