SQL Server “Denali” introduces a new feature called “SEQUENCE”. SEQUENCE is a user-defined object that generates a sequence of numeric values according to specified options.
Its not bound to a table like IDENTITY, You can use SEQUENCE across tables
Supported Data Types for creating SEQUENCE are as follows:
- TinyInt
- SmallInt
- Int
- BigInt
- Decimal
- Numeric
- /* Create Sequence With Start Values */
- CREATE SEQUENCE RunningNumbers
- START WITH 1
- INCREMENT BY 1;
- GO
The above code creates a simple SEQUENCE with starting number as 1 and increments by 1
To get the next number in SEQUENCE, you need to use “NEXT VALUE” option
- SELECT (NEXT VALUE FOR RunningNumbers) AS [RunningNumbers];
The below code might syntactically look same as the SEQUENCE "- RunningNumbers, but if you look at the output, its entirely different
- /* Create Sequence Without Start Values */
- CREATE SEQUENCE RunningNumbers2
- INCREMENT BY 1;
- GO
- SELECT (NEXT VALUE FOR RunningNumbers2) AS [RunningNumbers2];
If you see the output from RunningNumbers2, you will be getting a Negative value, its due to not specifying the Start number, so make sure you specify a Positive Start Number if you don’t want your table keys to Negative
Other options available in Creating Sequence are as follows:
- CREATE SEQUENCE RunningNumbers3
- AS tinyint
- START WITH 1
- INCREMENT BY 1
- MINVALUE 1
- MAXVALUE 5
- CYCLE ;
- GO
You can specify Minimum Value and Maximum Value and whether to Cycle the numbers when reaching maximum value or not
- SELECT NEXT VALUE FOR RunningNumbers3 AS ID, Name FROM sys.objects ;
- GO
If you see the below output, for every 5 records, the IDs are recycling and starts from 1 again
Restarting the Sequence
To restart the Sequence, you need to ALTER the sequence with RESTART option and start value
- /* Restart the Sequence */
- ALTER SEQUENCE [dbo].[RunningNumbers2]
- RESTART WITH 1
- SELECT (NEXT VALUE FOR RunningNumbers2) AS [RunningNumbers2];
Since we restarted the Sequence with 1, Now we are getting output as 1 for RunningNumber2
Find the available Sequences in a database
- SELECT
- object_id, name, type_desc, start_value, current_value, increment,
- minimum_value, maximum_value, is_cycling, is_cached, cache_size
- FROM sys.sequences
You can Sys.Sequence system view to query the list of available Sequences in a database
How to use it as Identity across tables ?
Create tables for each reqion
- — Create tables
- CREATE TABLE Orders_West
- (OrderID int PRIMARY KEY,
- Name varchar(20) NOT NULL,
- Qty int NOT NULL);
- GO
- CREATE TABLE Orders_East
- (OrderID int PRIMARY KEY,
- Name varchar(20) NOT NULL,
- Qty int NOT NULL);
- GO
- CREATE TABLE Orders_South
- (OrderID int PRIMARY KEY,
- Name varchar(20) NOT NULL,
- Qty int NOT NULL);
- GO
- CREATE TABLE Orders_North
- (OrderID int PRIMARY KEY,
- Name varchar(20) NOT NULL,
- Qty int NOT NULL);
- GO
Insert data to table using Sequences
- — Insert Four records
- INSERT Orders_West (OrderID, Name, Qty)
- VALUES (NEXT VALUE FOR RunningNumbers2, 'Apples', 2) ;
- INSERT Orders_East (OrderID, Name, Qty)
- VALUES (NEXT VALUE FOR RunningNumbers2, 'Oranges', 1) ;
- INSERT Orders_South (OrderID, Name, Qty)
- VALUES (NEXT VALUE FOR RunningNumbers2, 'Grapes', 1) ;
- INSERT Orders_North (OrderID, Name, Qty)
- VALUES (NEXT VALUE FOR RunningNumbers2, 'Banana', 1) ;
- GO
Query the inserted data
- SELECT * FROM Orders_West
- UNION
- SELECT * FROM Orders_East
- UNION
- SELECT * FROM Orders_North
- UNION
- SELECT * FROM Orders_South
If you see the Union output from 4 different tables, we are able to create a identity column across 4 tables, This will be very helpful when you horizontally partition data across tables
Delete the Sequences
- DROP SEQUENCE RunningNumbers;
- DROP SEQUENCE RunningNumbers2;
I hope you all find this information about Sequences useful and informative !!!