Getting a Range of Sequence Values

Getting a Range of Sequence Values: the T-SQL from this post in SQL Server Management Studio with its results

Applies to SQL Server 2019, 2022 and 2025.

When I need a block of numbers from a sequence in one go, I ask for a SQL Server sequence range with sys.sp_sequence_get_range rather than calling NEXT VALUE FOR in a loop. One call reserves the whole block, hands back the first and last values, and moves the sequence on so nobody else can be given the same numbers. It’s handy for loading a batch of invoices, or for an application that wants to number rows before it sends them to the server.

How a SQL Server sequence range works

You give the procedure the sequence name and how many values you want (@range_size, a bigint). It returns the first value of the block in @range_first_value, and can also return the last value, the increment, the minimum and maximum, and how many times the sequence has cycled. All the value outputs are sql_variant, so cast them back to the sequence’s own type before doing any sums with them.

Here’s a small, self-contained example. It creates an invoice sequence, puts five customers in a temp table, reserves exactly five numbers and hands them out in order:

CREATE SEQUENCE dbo.InvoiceNumber AS int START WITH 1000 INCREMENT BY 1;

CREATE TABLE #Batch (LineID int IDENTITY(1,1), Customer varchar(20));
INSERT INTO #Batch (Customer)
VALUES ('Acme'), ('Bolton Ltd'), ('Crane & Co'), ('Dyer Bros'), ('Ellis');

DECLARE @Need bigint = (SELECT COUNT(*) FROM #Batch),
        @First sql_variant,
        @Last sql_variant;

EXEC sys.sp_sequence_get_range
     @sequence_name     = N'dbo.InvoiceNumber',
     @range_size        = @Need,
     @range_first_value = @First OUTPUT,
     @range_last_value  = @Last OUTPUT;

SELECT b.Customer,
       CAST(@First AS int) + ROW_NUMBER() OVER (ORDER BY b.LineID) - 1 AS InvoiceNo,
       CAST(@Last AS int) AS RangeEnd
FROM #Batch AS b
ORDER BY b.LineID;

On my SQL Server 2025 test instance that gives Acme 1000 through to Ellis 1004, with 1004 as the end of the range. A NEXT VALUE FOR dbo.InvoiceNumber straight afterwards returns 1005: the sequence has already moved past the whole block, even though I haven’t inserted those invoices anywhere yet.

I use ROW_NUMBER() rather than the LineID itself because identity columns can have gaps, and I want the invoice numbers to line up exactly with the block I reserved. If you’re loading rows into a real table, the same pattern works inside an INSERT ... SELECT.

Why not just use NEXT VALUE FOR?

For set-based inserts, NEXT VALUE FOR is fine: an INSERT ... SELECT gets a new value per row. Where a range helps is when the numbers are needed before the insert, or outside SQL Server altogether, such as an application building parent and child rows that refer to each other. One round trip for a block of 500 beats 500 round trips.

One thing that surprises people: if you reference NEXT VALUE FOR on the same sequence twice in one SELECT, both columns get the same value for that row. It’s one value per row per sequence, not one per mention.

The common mistake: expecting rolled-back values to come back

Sequences aren’t transactional. Once sp_sequence_get_range has handed out a block, rolling back the transaction it ran in doesn’t return the numbers. They’re simply gone, and the sequence carries on from after the block:

DECLARE @First sql_variant;

BEGIN TRANSACTION;
EXEC sys.sp_sequence_get_range
     @sequence_name     = N'dbo.InvoiceNumber',
     @range_size        = 100,
     @range_first_value = @First OUTPUT;
ROLLBACK TRANSACTION;

SELECT CAST(@First AS int) AS RolledBackFirst,
       NEXT VALUE FOR dbo.InvoiceNumber AS NextAfterRollback;

Run straight after the first example, that shows a rolled-back block starting at 1005 and a next value of 1105. A hundred invoice numbers have vanished. The same applies to a failed load, a batch that only uses 40 of the 50 numbers it asked for, and to plain NEXT VALUE FOR.

This matters when someone says invoice or cheque numbers must have no gaps. A sequence (or an identity column) can’t promise that. If gap-free numbering is a real business rule, you need a numbering table updated inside the same transaction as the insert, and you accept that it serialises the inserts. Usually the better answer is to agree that gaps are fine and keep a log of what was voided.

Watch out for

  • The increment. My first + row - 1 sum only works because the sequence goes up by 1. For a sequence created with INCREMENT BY 10, a range of three starting at 10 is 10, 20 and 30. Ask for @sequence_increment as an output and multiply by it.
  • Running out. If the block would go past the maximum and the sequence doesn’t cycle, you get error 11732 and nothing is reserved. With CYCLE, the block can wrap round to the minimum, so check @range_cycle_count before assuming the numbers run in a straight line.
  • The sql_variant outputs. Cast them to the sequence’s data type before arithmetic or comparisons.
  • Permissions. The caller needs UPDATE permission on the sequence, not just SELECT, because the procedure moves it on.

The full parameter list is on Microsoft Learn under sp_sequence_get_range (Transact-SQL), and the Sequence Numbers page covers the sequence object itself. If you’re loading small batches by hand, my post on the table value constructor pairs well with this, and there’s more on day-to-day T-SQL in the SQL Server DBA Handbook.

This topic came up in the SQLServerCentral Question of the Day: Multiple Sequence Values.

Clive Roberts DBA