Sequence Objects

Sequence is a type of user-defined object that generates a sequence of numeric values. We can use a Sequence Object for T-SQL queries. A Sequence Object starts with a specific value. The value of a Sequence Object can be increase or decrease with a specific interval. Sequence Objects have an option for a minimum and maximum value. Applications refer to a Sequence Object to retrieve its next value. The relationship between sequences and tables is controlled by the application. Sequences, unlike identity columns, are not associated with specific tables.

Syntax

  1. CREATE SEQUENCE [schema_name . ] sequence_name
  2. [ AS [ built_in_integer_type | user-defined_integer_type ] ]
  3. [ START WITH <constant> ]
  4. [ INCREMENT BY <constant> ]
  5. [ { MINVALUE [ <constant> ] } | { NO MINVALUE } ]
  6. [ { MAXVALUE [ <constant> ] } | { NO MAXVALUE } ]
  7. [ CYCLE | { NO CYCLE } ]
  8. [ { CACHE [ <constant> ] } | { NO CACHE } ]
  9. [ ; ]
Arguments

sequence_name: define a unique name for a Sequence Object.

[ built_in_integer_type | user-defined_integer_type ]: defines the data type for a Sequence Object. A sequence can be defined as any integer type. If no data type is provided, the bigint data type is used as the default. The following types are allowed:

Data Type

Range

Tinyint 0 to 255
Smallint -32,768 to 32,767
Int -2,147,483,648 to 2,147,483,647
Bigint -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

START WITH: It is a type of constant. The first value returned by the Sequence Object. The START value must be a value less than or equal to the maximum and greater than or equal to the minimum value of the Sequence Object.

INCREMENT BY: It is a type of constant. It can be either a positive or negative value. If value is a negative number then the value will be an ascending sequence else the value will be an descending sequence.

MINVALUE:
It defines a minimum value allowed for the sequence. The default minimum value for a new Sequence Object is the minimum value of the data type of the Sequence Object.

MAXVALUE:
It defines a maximum value allowed for the sequence. The default maximum value for a new Sequence Object is the maximum value of the data type of the Sequence Object.

[ CYCLE | NO CYCLE ]: Cycle means that the sequence will start over once it has completed the sequence. NO CYCLE means that the sequence will raise an error when it has completed the sequence. It will not start the sequence over again.

[ CACHE [<constant> ] | NO CACHE ]: Increases performance for applications that use Sequence Objects by minimizing the number of disk operations required to generate sequence numbers. Defaults to CACHE.

For example, if a cache size of 50 is chosen, SQL Server does not keep 50 individual values cached. It only caches the current value and the number of values left in the cache. This means that the amount of memory required to store the cache is always two instances of the data type of the Sequence Object.

Now for a better understanding we will consider some examples.

Example 1

  1. CREATE SEQUENCE Sequence_Example
  2. START WITH 1
  3. INCREMENT BY 1;
In the preceding example, we created a sequence with the name Sequemce_Example. In this example we don't define the data type for the sequence. So SQL Server uses the default datatype (in other words bigint). The starting value of the sequence is 1 . We don't define the minimum and maximum value for Sequence, so SQL Server will use the minimum and maximum value of datatype (in other words minimum = -9,223,372,036,854,775,808 , maximum = 9,223,372,036,854,775,807). We don't define anything about the Cache Option, so SQL Server uses the Cache option by default for the sequence.

Example 2

  1. CREATE SEQUENCE Sequence_Examle
  2. AS SMALLINT
  3. START WITH 1
  4. INCREMENT BY 1
  5. MINVALUE 1
  6. MAXVALUE 99
  7. NO CYCLE
  8. CACHE 10;
In the preceding example, we created a sequence with the name Sequence_Example. The data type of the sequence is a smallint. Sequence will start from 1 and each time it increases by 1 (in other words 2, 3, 4, 5…). Minimum value of sequence is 1 and maximum value is 99.

We define the cache size of 10, so it will cache the 10 value at a time to increase the performance. We use the No CYCLE option for sequence so the sequence will truncate after obtaining the maximum value.

Example 3
  1. CREATE SEQUENCE Sequence_Example
  2. AS SMALLINT
  3. START WITH 1
  4. INCREMENT BY 1
  5. MINVALUE 1
  6. MAXVALUE 99
  7. NO CYCLE
  8. CACHE 10;
  9. SELECT NEXT VALUE FOR Sequence_Example AS [sequence]
Output

see result

The “SELECT NEXT VALUE FOR Sequence_Object” statement is used for retrieving the next value from the Sequence Object.

Example 4
  1. CREATE SEQUENCE Sequence_Example
  2. AS SMALLINT
  3. START WITH 1
  4. INCREMENT BY 1
  5. MINVALUE 1
  6. MAXVALUE 99
  7. NO CYCLE
  8. CACHE 10;
  9. SELECT NEXT VALUE FOR Sequence_Example AS [sequence] UNION ALL
  10. SELECT NEXT VALUE FOR Sequence_Example UNION ALL
  11. SELECT NEXT VALUE FOR Sequence_Example UNION ALL
  12. SELECT NEXT VALUE FOR Sequence_Example
Output

message

This example illustrates that we cannot use Distinct, Union, Union All, Intersect and Except operator with a Sequence Object.

Example 5

  1. CREATE TABLE #TEMP
  2. (
  3. IID INT IDENTITY(1,1),
  4. SEQUENCE INT
  5. );
  6. DECLARE @INT INT;
  7. SET @INT=1;
  8. WHILE(@INT<=10)
  9. BEGIN
  10. INSERT INTO #TEMP
  11. VALUES
  12. (
  13. NEXT VALUE FOR Sequence_Example
  14. )
  15. SET @INT=@INT+1;
  16. END
  17. SELECT * FROM #TEMP t
  18. DROP TABLE #TEMP
Output

TEMP

This example shows how to insert the data into a table from a Sequence Object.

Example 6: Drop Sequence
  1. IF EXISTS (SELECT * FROM sys.sequences WHERE name = N'Sequence_Example')
  2. DROP SEQUENCE Sequence_Example;
This example shows that we can drop a Sequence Object using the Drop Command.

Example 7
  1. CREATE SEQUENCE Sequence_Example
  2. AS SMALLINT
  3. START WITH 1
  4. INCREMENT BY 1
  5. MINVALUE 1
  6. MAXVALUE 5
  7. NO CYCLE
  8. CACHE 5;
  9. CREATE TABLE #TEMP
  10. (
  11. IID INT IDENTITY(1,1),
  12. SEQUENCE INT
  13. );
  14. DECLARE @INT INT;
  15. SET @INT=1;
  16. WHILE(@INT<=10)
  17. BEGIN
  18. INSERT INTO #TEMP
  19. VALUES
  20. (
  21. NEXT VALUE FOR Sequence_Example
  22. )
  23. SET @INT=@INT+1;
  24. END
  25. SELECT * FROM #TEMP t
  26. DROP TABLE #TEMP
Output

result

If we execute the preceding code, then SQL Server will throw an error. Because a Sequence Object has a maximum value 5 and we are exceeding this value.

So, to overcome this problem we can use the Cycle option for a Sequence Object.

Example 8 (Sequence with Cycle Option)

  1. CREATE SEQUENCE Sequence_Example
  2. AS SMALLINT
  3. START WITH 1
  4. INCREMENT BY 1
  5. MINVALUE 1
  6. MAXVALUE 5
  7. CYCLE
  8. CACHE 5;
  9. CREATE TABLE #TEMP
  10. (
  11. IID INT IDENTITY(1,1),
  12. SEQUENCE INT
  13. );
  14. DECLARE @INT INT;
  15. SET @INT=1;
  16. WHILE(@INT<=10)
  17. BEGIN
  18. INSERT INTO #TEMP
  19. VALUES
  20. (
  21. NEXT VALUE FOR Sequence_Example
  22. )
  23. SET @INT=@INT+1;
  24. END
  25. SELECT * FROM #TEMP t
  26. DROP TABLE #TEMP
Output

SELECT

In this example we define the maximum value equal to 5 and the Sequence Object contains a Cycle option so the Sequence Object will restart after reaching the maximum value and the values will repeat after a definite interval.

Example 9 (Restart Sequence)
  1. IF EXISTS (SELECT * FROM sys.sequences WHERE name = N'Sequence_Example')
  2. DROP SEQUENCE Sequence_Example;
  3. CREATE SEQUENCE Sequence_Example
  4. AS SMALLINT
  5. START WITH 1
  6. INCREMENT BY 1
  7. MINVALUE 1
  8. MAXVALUE 5
  9. CYCLE
  10. CACHE 5;
  11. CREATE TABLE #TEMP
  12. (
  13. IID INT IDENTITY(1,1),
  14. SEQUENCE INT
  15. );
  16. DECLARE @INT INT;
  17. SET @INT=1;
  18. WHILE(@INT<=10)
  19. BEGIN
  20. IF @INT%4=0
  21. BEGIN
  22. ALTER SEQUENCE Sequence_Example /* Restart Sequence */
  23. RESTART;
  24. END
  25. INSERT INTO #TEMP
  26. VALUES
  27. (
  28. NEXT VALUE FOR Sequence_Example
  29. )
  30. SET @INT=@INT+1;
  31. END
  32. SELECT * FROM #TEMP t
  33. DROP TABLE #TEMP
Output

Restart Sequence

This example shows that we can restart a Sequence Object at any time. To restart the current value of a Sequence Object to its initial or specified value, use the ALTER SEQUENCE statement.

Example 10
  1. IF EXISTS (SELECT * FROM sys.sequences WHERE name = N'Sequence_Example')
  2. DROP SEQUENCE Sequence_Example;
  3. CREATE SEQUENCE Sequence_Example
  4. AS SMALLINT
  5. START WITH 1
  6. INCREMENT BY 5
  7. MINVALUE 1
  8. MAXVALUE 50
  9. CYCLE
  10. CACHE 5;
  11. CREATE TABLE #TEMP
  12. (
  13. IID INT IDENTITY(1,1),
  14. SEQUENCE INT
  15. );
  16. DECLARE @INT INT;
  17. SET @INT=1;
  18. WHILE(@INT<=10)
  19. BEGIN
  20. IF @INT%4=0
  21. BEGIN
  22. ALTER SEQUENCE Sequence_Example /* Restart with a specific value */
  23. RESTART WITH 10 ;
  24. END
  25. INSERT INTO #TEMP
  26. VALUES
  27. (
  28. NEXT VALUE FOR Sequence_Example
  29. )
  30. SET @INT=@INT+1;
  31. END
  32. SELECT * FROM #TEMP t
  33. DROP TABLE #TEMP
Output

LTER SEQUENCE

This example shows that we can restart any Sequence Object with a specific value. In the preceding example we can see that when the Sequence Object restarts, it's start value will be 10 each time.

Example 11 (Sequence with descending order)
  1. IF EXISTS (SELECT * FROM sys.sequences WHERE name = N'Sequence_Example')
  2. DROP SEQUENCE Sequence_Example;
  3. CREATE SEQUENCE Sequence_Example
  4. AS SMALLINT
  5. START WITH 10
  6. INCREMENT BY -1
  7. MINVALUE 1
  8. MAXVALUE 50
  9. CYCLE
  10. CACHE 5;
  11. CREATE TABLE #TEMP
  12. (
  13. IID INT IDENTITY(1,1),
  14. SEQUENCE INT
  15. );
  16. DECLARE @INT INT;
  17. SET @INT=1;
  18. WHILE(@INT<=10)
  19. BEGIN
  20. INSERT INTO #TEMP
  21. VALUES
  22. (
  23. NEXT VALUE FOR Sequence_Example
  24. )
  25. SET @INT=@INT+1;
  26. END
  27. SELECT * FROM #TEMP t
  28. DROP TABLE #TEMP
Output

DROP TABLE

Example 12 (Sequence Information)


Using system view sys.sequences we can retrieve information about any sequence. A sys.sequences view can obtain the following information about the Sequence Object.

Sequence Information

The syntax to a view and the properties of a sequence in SQL Server (Transact-SQL) is:
  1. SELECT s.name ,s.type ,s.minimum_value,s.maximum_value,s.create_date,s.modify_date
  2. FROM sys.sequences s
  3. WHERE name = 'Sequence_Example';
Output

Output