Narek Nersisyan
Narek Nersisyan

Reputation: 75

How to generate consecutive integers in Azure SQL?

I am interested in generating a series of consecutive integers from 1 to 1000 (for example) and storing these numbers in each row of some table. I would like to do this in Microsoft Azure SQL but I am not sure if arrays are even supported.

Upvotes: 0

Views: 79

Answers (2)

Grant Fritchey
Grant Fritchey

Reputation: 2775

Another mechanism to solve something like this could be to use a SEQUENCE on the table. It's similar to an IDENTITY column (they actually have the same behavior under the covers) without some of the restrictions. Just reset it to a new seed value as you add data to the table.

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269873

One relatively simple method is a recursive CTE:

with n as (
      select 1 as n
      union all
      select n + 1
      from n
      where n < 1000
     )
select n.n
from n
options (maxrecursion 0);

Upvotes: 3

Related Questions