3

I want to create a sequence of numbers in SQL Server that will have a minimum value and maximum value. I want to cycle if the number reaches the maximum limit. Can any body help me??

2
  • 1) Use a WHILE loop, and 2) you should take this over to stackoverflow.com Commented Dec 1, 2009 at 13:53
  • It all depends on how you are planning to use the sequence. All of the answers thus far make some assumptions. Commented Sep 29, 2016 at 17:28

4 Answers 4

1

There is no need for a while loop. First, you need a Tally or Numbers table:

Create Table dbo.Numbers ( Value int not null Primary Key Clustered )
GO
With Nums As
    (
    Select Row_Number() Over( Order By S1.object_id ) As Num
    From sys.columns as s1
        cross join sys.columns as s2
    )
Insert dbo.Numbers( Value )
Select Num
From Nums
Where Num <= 100000

I only put a 100K of numbers in my table but you might need more. You need only populate this table once.Now you can create any sequence you desire. Like so:

Select Value
From dbo.Numbers
Where Value Between @Start And @End

Want an increment value?:

Select Value
From dbo.Numbers
Where Value % @Increment = 0
Sign up to request clarification or add additional context in comments.

1 Comment

Agreed. Numbers / tally tables are very useful for so many features - it may appear overkill for this requirement but there are so many other places where you'll find uses for them that I wouldn't bother with other solutions because you'll need this table sooner or later anyway.
1

As of SQL Server 2012, sequences have been added and are supposedly faster than IDENTITY.

Quick example using CYCLE option as the question requests but most people probably will not use CYCLE because they'll use it for a synthetic primary key.

CREATE SEQUENCE Schema.SequenceName
AS int
INCREMENT BY 1 
CYCLE;

Here's the SQL syntax taken from MSDN:

CREATE SEQUENCE [schema_name . ] sequence_name  
    [ AS [ built_in_integer_type | user-defined_integer_type ] ]  
    [ START WITH <constant> ]  
    [ INCREMENT BY <constant> ]  
    [ { MINVALUE [ <constant> ] } | { NO MINVALUE } ]  
    [ { MAXVALUE [ <constant> ] } | { NO MAXVALUE } ]  
    [ CYCLE | { NO CYCLE } ]  
    [ { CACHE [ <constant> ] } | { NO CACHE } ]  
    [ ; ]  

Here's how to do it in the GUI. This is the equivolent of the quick example above without the CYCLE option checked because most poeple won't use that:

  1. In the Object Explorer, expand the Programmability folder
  2. Under the Programmability folder, right click on the Sequences folder as shown below:

enter image description here

  1. Underlined are the values that you would update to get the equivalent of the SQL statement above, however, I would consider changing these depending on your needs (see notes below).

enter image description here

Notes:

  • The default Start value, Minimum value, and Maximum value were determined by the range of the data type which was an int in this case. See here for more info on sequences and ranges.
  • Pretty decent chance you'll want your sequence to start at 1 and you might want you minimum value as 1, too.

Comments

0

Here is some code that works in SQL 2000:

DECLARE @NumRows int;
SET @NumRows = 10000;
DECLARE @t table (RowNum int not null primary key);

-- Seed
INSERT @t VALUES(1)

WHILE @@ROWCOUNT > 0
BEGIN
    INSERT @t
     SELECT     t.RowNum + x.MaxRowNum FROM @t t
     CROSS JOIN 
     (SELECT    MAX(RowNum) MaxRowNum FROM @t) x
     WHERE      t.RowNum <= @NumRows - x.MaxRowNum
END

Adapted from this: Dr. Tom's Workshop: Generating Sequence Numbers

1 Comment

This loop will be increasingly slower than window function/CTE the more rows you need.
-1

I would like to create a sequence starting from 1

CREATE SEQUENCE if not exists sequence_name START 1

It will work for you...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.