Sequence Objects in SQL Server 2012 and Later

In this article, we will be discussing about Sequence objects in SQL Server 2012 and later, and see a relevant example.

In an older article on our blog, we wrote on how someone can mimic the operation of Sequence Objects using other alternative ways.

However, since SQL Server 2012 and later, Sequence Objects are available for use, thus making easier the whole process.

So, how can we use a Sequence Object in SQL Server?

 

SQL Server Sequence Object Example

Now, let’s see some examples of using Sequence Objects in SQL Server 2012 and later.

Select the proper database

USE [DATABASE_NAME];
GO

 

Create the Sequence Object (TestSeq is my Sequence’s name)

CREATE SEQUENCE TestSeq
    AS bigint
START WITH 0
    INCREMENT BY 1
GO

 

To get the next value from the Sequence

SELECT NEXT VALUE FOR TestSeq;
GO

To drop the sequence object

DROP SEQUENCE TestSeq;
GO

 

To reset the sequence object

--You can replace 0 with any other constant value
ALTER SEQUENCE TestSeq
RESTART WITH 0
GO
Hope this helps!

 

Strengthen you SQL Server Development Skills – Enroll to our Online Course!

Check our online course titled “Essential SQL Server Development Tips for SQL Developers
(special limited-time discount included in link).

Via the course, you will sharpen your SQL Server database programming skills via a large set of tips on T-SQL and database development techniques. The course, among other, features over than 30 live demonstrations!

Essential SQL Server Development Tips for SQL Developers - Online Course
(Lifetime Access/ Live Demos / Downloadable Resources and more!)

Learn More

 

 

Did you find this article useful and interesting? Find hundreds of useful SQL Server programming/development articles in my eBook: “Developing with SQL Server (Second Edition“.

Check our other related SQL Server Development articles.

Check out our latest software releases!

Subscribe to our newsletter and stay up to date!

 

Featured Online Courses:

 

Check our Related SQL Server Development Articles:

 

Rate this article: 1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)

Loading...

Reference: SQLNetHub.com (https://www.sqlnethub.com)

© SQLNetHub