in SQL Server, if I have an int variable, how can I write a query that returns the values 1 through the int variable’s value?
So given:
declare @cnt int
set @cnt = 5
What query would return 1 – 5? Is this possible?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
An alternative answer to the others already given, maybe not so practical but it may be faster in certain situations.
If you have a known upper bound for the values of @cnt you will encounter (say 1000) you could create a simple one-column table with the first 1000 integers in it, like
and then whenever you want a set of integers you could get it using