I need to select all N numbers (integers) between @min and @max Is there any way to achieve this without using any loop?
Example: Let them say that @min = 5, @max = 9
I need the following values ββreturned by my SQL query: 5,6,7,8,9
(I am using MSSQL 2005)
Thanks!!
EDIT: This is a solution using a custom function that works fine. But there seems to be too much effort to sort through all the numbers manually. Thus, the question remains valid, achievable without a cycle.
CREATE FUNCTION GetAllNBetween ( @Min int, @Max int ) RETURNS @N TABLE(n int) AS BEGIN WHILE @Min <= @Max BEGIN INSERT INTO @N VALUES(@Min) SET @Min = @Min + 1 END RETURN END
Used as follows:
SELECT * FROM GetAllNBetween(5, 9)
source share