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
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!
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:
- SQL Server Fundamentals – SQL Database for Beginners
- Essential SQL Server Administration Tips
- Essential SQL Server Development Tips for SQL Developers
- SQL Server 2019: What’s New – New and Enhanced Features
- How to Import and Export Data in SQL Server Databases
- Introduction to Azure SQL Database for Beginners
- Introduction to Azure Database for MySQL
- Working with Python on Windows and SQL Server Databases
- Boost SQL Server Database Performance with In-Memory OLTP
- Introduction to Computer Programming for Beginners
- .NET Programming for Beginners – Windows Forms with C#
- Introduction to SQL Server Machine Learning Services
- Entity Framework: Getting Started – Complete Beginners Guide
- Learn How to Install and Start Using SQL Server in 30 Mins
- A Guide on How to Start and Monetize a Successful Blog
Check our Related SQL Server Development Articles:
- Essential SQL Server Development Tips for SQL Developers
- How to Import and Export Data in SQL Server
- Listing all Tables of a Linked Server’s Database
- How to Import and Export Unstructured Data in SQL Server – FileTables
- How to Import and Export Unstructured Data in SQL Server – FILESTREAM
- How to Import and Export Unstructured Data in SQL Server – The IMAGE Datatype
- …more
Rate this article:
Reference: SQLNetHub.com (https://www.sqlnethub.com)
© SQLNetHub
Artemakis Artemiou is a seasoned Senior Database and AI/Automation Architect with over 20 years of expertise in the IT industry. As a Certified Database, Cloud, and AI professional, he has been recognized as a thought leader, earning the prestigious Microsoft Data Platform MVP title for nine consecutive years (2009-2018). Driven by a passion for simplifying complex topics, Artemakis shares his expertise through articles, online courses, and speaking engagements. He empowers professionals around the globe to excel in Databases, Cloud, AI, Automation, and Software Development. Committed to innovation and education, Artemakis strives to make technology accessible and impactful for everyone.