Click here to Skip to main content
15,891,657 members
Articles / Database Development / SQL Server / SQL Server 2012
Tip/Trick

Sequence Object in SQL Server 2012

Rate me:
Please Sign up or sign in to vote.
4.95/5 (8 votes)
23 Nov 2015CPOL2 min read 15.1K   11   3
A sequence is a user defined, schema bound object that generates a sequence of numeric values

Introduction

A sequence is a user defined, schema bound object that generates a sequence of numeric values (in ascending or descending order) according to specification. Unlike identity columns, a sequence is created independent of any table. A few interesting differences between the two are;

  • A Sequence object is independent of any table, whereas the Identity column property is table specific
  • Unlike Identity, you can generate a new sequence value before using it in an insert Statement
  • You can define both the minimum & maximum values, as well as cycling & cache size options for a sequence
  • Unlike Identity, a sequence object will increment its value only when it is explicitly called

Background

Microsoft introduced sequence object in SQL Server 2012 release. 

Using the code

Let's create a sequence object and see how it works.

SQL
CREATE SEQUENCE dbo.emp_sequence AS INT
START WITH 1
INCREMENT BY 1
MINVALUE 1

We have created a sequence with name emp_sequence where the starting value will be 1 and next value will be incremented by 1.

Now lets fetch first value for the sequence.

SQL
SELECT NEXT VALUE FOR dbo.emp_sequence as first_emp

OUTPUT

 

 

Check the output where first value is fetched as 1.

Now lets fetch next value for the sequence 

SQL
SELECT NEXT VALUE FOR dbo.emp_sequence as next_emp

OUTPUT


 

 

 

The next value is fetched as 2 which is incremented by 1 from the previous value.

Now let's see how a sequence object can be used with table and how can act as alternative to identity. 

For this, we are going to use the same sequence we created in the earlier example, so lets first reset the initial value of sequence

SQL
ALTER SEQUENCE dbo.emp_sequence
RESTART WITH 1
INCREMENT BY 1;

Now lets create a temp table employee where we will insert the data in emp_no column using sequence

SQL
CREATE TABLE #employee (emp_no int, emp_name VARCHAR(10))
INSERT INTO #employee VALUES
  (NEXT VALUE FOR dbo.emp_sequence, 'Ajay')
, (NEXT VALUE FOR dbo.emp_sequence, 'Vijay')
, (NEXT VALUE FOR dbo.emp_sequence, 'Sanjay')

SELECT * FROM #employee
<span style="font-size: 9pt;">DROP TABLE #employee</span>

OUTPUT 

 

 

 

 

 

Check the output where emp_no is generated sequentially as 1,2,3 using sequence. 

In the script we have also dropped the temp table, but the current value of sequence has been incremented to 3.

Let's run the same script again

SQL
CREATE TABLE #employee (emp_no int, emp_name VARCHAR(10));
INSERT INTO #employee VALUES
  (NEXT VALUE FOR dbo.emp_sequence, 'Ajay')
, (NEXT VALUE FOR dbo.emp_sequence, 'Vijay')
, (NEXT VALUE FOR dbo.emp_sequence, 'Sanjay')

SELECT * FROM #employee
DROP TABLE #employee

OUTPUT 

 

 

 

 

 

Check the output where emp_no is generated as 4,5,6 using the same script. This is because the value of sequence was 3.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionMicrosoft Catching up Pin
Juzer21-Nov-15 13:00
Juzer21-Nov-15 13:00 
AnswerRe: Microsoft Catching up Pin
PIEBALDconsult23-Nov-15 16:02
mvePIEBALDconsult23-Nov-15 16:02 
GeneralRe: Microsoft Catching up Pin
sandeepmittal1123-Nov-15 17:11
sandeepmittal1123-Nov-15 17:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.