Sequence is a unique set of numerical values 1, 2, 3, ... which are generated based on needs.
CREATE SEQUENCE sequence_name START WITH integer_value INCREMENT BY integer_value MINVALUE minimum_value MAXVALUE maximum_value CYCLE|NOCYCLE
sequence_name : Unique name of the sequence.
minimum_value: Minimum value of the sequence.
maximum_value: Maximum value of the sequence.
cycle: It continues the same sequence once it reaches the maximum value. Basically, once the maximum value is reached it resets the next value as minimum value.
nocycle: Throws an error message when it reaches the maximum value.
Example
Let's create a sequence named "prod_sequence" like mentioned in below statement,
CREATE SEQUENCE prod_sequence START WITH 1 INCREMENT BY 1 MINVALUE 1 MAXVALUE 5 NO CYCLE
After the sequence has been created to see the properties of the sequence use below statement,
SELECT * FROM sys.sequences WHERE name = 'prod_sequence';
Using Sequence
The below example shows how to use the sequence while inserting a row to a table.
INSERT into tbl_products VALUES(next value for prod_sequence,'Alexa Eco Dot');
NEXT VALUE FOR function is used to retrieve the next value of a sequence and it cannot be used when a database is in read-only mode.
Comments