Generating Numbers with Sequences
This lesson explains sequence objects, how they differ from IDENTITY columns, and how caching causes gaps.
Learning outcomes
After completing this lesson, you will be able to:
- Describe the options of CREATE SEQUENCE and how NEXT VALUE FOR is used.
- Distinguish between sequences and IDENTITY columns.
- Explain why neither generates gap-free numbers.
Creating and using a sequence
CREATE SEQUENCE defines the data type (any integer type or decimal with scale 0), START WITH, INCREMENT BY (which can be negative), MINVALUE, MAXVALUE, whether it CYCLEs back to the minimum when exhausted, and the CACHE size. NEXT VALUE FOR returns the next number and can be used in a SELECT, an INSERT, a variable assignment or a column DEFAULT.
CREATE SEQUENCE Billing.InvoiceNumber AS bigint
START WITH 100000 INCREMENT BY 1 CACHE 50;
-- Shared by two tables through defaults
ALTER TABLE Billing.RetailInvoice ADD CONSTRAINT DF_Retail_No
DEFAULT (NEXT VALUE FOR Billing.InvoiceNumber) FOR InvoiceNo;
ALTER TABLE Billing.WholesaleInvoice ADD CONSTRAINT DF_Wholesale_No
DEFAULT (NEXT VALUE FOR Billing.InvoiceNumber) FOR InvoiceNo;
-- Get a number before inserting, for example to print it first
DECLARE @no bigint = NEXT VALUE FOR Billing.InvoiceNumber;A sequence that runs out
Sequences compared with IDENTITY
IDENTITY and sequences both generate numbers, but they differ in scope and control, as the following comparison shows.
IDENTITY or SEQUENCE?
Caching and gaps
With CACHE n, the engine allocates n values in memory and writes only the last one to metadata, which makes NEXT VALUE FOR fast; after an unexpected shutdown, unused cached values are lost and a gap appears. Values consumed by a rolled-back transaction are also never reused. Neither IDENTITY nor sequences guarantee gap-free numbers, so requirements such as legally consecutive invoice numbers need a different design, for example assigning numbers in a serialized step at posting time. sys.sequences shows each sequence's current value and settings.
Numbering across two tables
Retail and wholesale invoices are stored in different tables but must share one series of invoice numbers, and the number must be printed on a PDF before the invoice row is written. Which design meets both requirements?
Create one sequence, call NEXT VALUE FOR to obtain the number for the PDF, and insert it into whichever table applies (or let both tables default to the sequence). An IDENTITY column belongs to one table, so two tables would produce overlapping numbers, and its value is known only after the insert.
Why can a cached sequence skip numbers after an unexpected restart?
Show answer
Values allocated in memory but not yet used are lost, so the sequence continues after the cached range.
Exam Tips
Remember for the exam
- Shared series across tables or the number before the insert: SEQUENCE.
- Reserve a block of values: sp_sequence_get_range.
- CACHE improves speed but can leave gaps after a crash.
- Neither IDENTITY nor SEQUENCE guarantees gap-free numbers.