Articles → SQL SERVER → Sequence Objects In SQL Server

Sequence Objects In SQL Server






Purpose





How The Sequence Objects Are Different From The Identity Column?





Syntax


CREATE SEQUENCE [schema].[Name_of_Sequence]
  [ AS <data type> ]
  [ START WITH <value> ]
  [ INCREMENT BY <value> ]
  [ MINVALUE <value >]
  [ MAXVALUE <value>]


ParameterDescription
Data typeSpecifies the data type of the sequence. Data types can be Decimal, Int, SmallInt, TinyInt, and BigInt
START WITHSets the starting value for the sequence object
INCREMENT BYSets the amount that you want your sequence object to increment by
MIN VALUEThis is an optional parameter that specifies the minimum value for the sequence object
MAX VALUEThis is an optional parameter that sets the maximum value for the sequence object



Example


CREATE SEQUENCE [dbo].[MyCounter] 
 AS [int]
 START WITH 1
 INCREMENT BY 1
 MINVALUE 1
 MAXVALUE 20



Get Next Value Of The Sequence




SELECT NEXT VALUE FOR [dbo].[MyCounter]





Altering The Sequence




ALTER SEQUENCE [MyCounter]
MAXVALUE 25





Posted By  -  Karan Gupta
 
Posted On  -  Monday, October 9, 2023

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250