NEXT VALUE FOR (Transact-SQL)

**APPLIES TO:** ![yes](media/yes.png)SQL Server (starting with 2012) ![yes](media/yes.png)Azure SQL Database ![no](media/no.png)Azure SQL Data Warehouse ![no](media/no.png)Parallel Data Warehouse

Generates a sequence number from the specified sequence object.

For a complete discussion of both creating and using sequences, see Sequence Numbers. Use sp_sequence_get_range to generate reserve a range of sequence numbers.

Topic link icon Transact-SQL Syntax Conventions

Syntax

  
NEXT VALUE FOR [ database_name . ] [ schema_name . ]  sequence_name  
   [ OVER (<over_order_by_clause>) ]  

Arguments

database_name
The name of the database that contains the sequence object.

schema_name
The name of the schema that contains the sequence object.

sequence_name
The name of the sequence object that generates the number.

over_order_by_clause
Determines the order in which the sequence value is assigned to the rows in a partition. For more information, see OVER Clause (Transact-SQL).

Return Types

Returns a number using the type of the sequence.

Remarks

The NEXT VALUE FOR function can be used in stored procedures and triggers.

When the NEXT VALUE FOR function is used in a query or default constraint, if the same sequence object is used more than once, or if the same sequence object is used both in the statement supplying the values, and in a default constraint being executed, the same value will be returned for all columns referencing the same sequence within a row in the result set.

The NEXT VALUE FOR function is nondeterministic, and is only allowed in contexts where the number of generated sequence values is well defined. Below is the definition of how many values will be used for each referenced sequence object in a given statement:

Limitations and Restrictions

The NEXT VALUE FOR function cannot be used in the following situations:

Using a Sequence Object in a Default Constraint

When using the NEXT VALUE FOR function in a default constraint, the following rules apply:

Using a Sequence Object with an OVER ORDER BY Clause

The NEXT VALUE FOR function supports generating sorted sequence values by applying the OVER clause to the NEXT VALUE FOR call. By using the OVER clause, a user is guaranteed that the values being returned are generated in the order of the OVER clause’s ORDER BY subclause. The following additional rules apply when using the NEXT VALUE FOR function with the OVER clause:

Metadata

For information about sequences, query the sys.sequences catalog view.

Security

Permissions

Requires UPDATE permission on the sequence object or the schema of the sequence. For an example of granting permission, see example F later in this topic.

Ownership Chaining

Sequence objects support ownership chaining. If the sequence object has the same owner as the calling stored procedure, trigger, or table (having a sequence object as a default constraint), no permission check is required on the sequence object. If the sequence object is not owned by the same user as the calling stored procedure, trigger, or table, a permission check is required on the sequence object.

When the NEXT VALUE FOR function is used as a default value in a table, users require both INSERT permission on the table, and UPDATE permission on the sequence object, to insert data using the default.

Audit

To audit the NEXT VALUE FOR function, monitor the SCHEMA_OBJECT_ACCESS_GROUP.

Examples

For examples of both creating sequences and using the NEXT VALUE FOR function to generate sequence numbers, see Sequence Numbers.

The following examples use a sequence named CountBy1 in a schema named Test. Execute the following statement to create the Test.CountBy1 sequence. Examples C and E use the **AdventureWorks2012** database, so the CountBy1 sequence is created in that database.

USE AdventureWorks2012 ;  
GO  
  
CREATE SCHEMA Test;  
GO  
  
CREATE SEQUENCE Test.CountBy1  
    START WITH 1  
    INCREMENT BY 1 ;  
GO  

A. Using a sequence in a select statement

The following example creates a sequence named CountBy1 that increases by one every time that it is used.

SELECT NEXT VALUE FOR Test.CountBy1 AS FirstUse;  
SELECT NEXT VALUE FOR Test.CountBy1 AS SecondUse;  

Here is the result set.

FirstUse  
1  
 
SecondUse  
2

B. Setting a variable to the next sequence value

The following example demonstrates three ways to set a variable to the next value of a sequence number.

DECLARE @myvar1 bigint = NEXT VALUE FOR Test.CountBy1  
DECLARE @myvar2 bigint ;  
DECLARE @myvar3 bigint ;  
SET @myvar2 = NEXT VALUE FOR Test.CountBy1 ;  
SELECT @myvar3 = NEXT VALUE FOR Test.CountBy1 ;  
SELECT @myvar1 AS myvar1, @myvar2 AS myvar2, @myvar3 AS myvar3 ;  
GO  

C. Using a sequence with a ranking window function

USE AdventureWorks2012 ;  
GO  
  
SELECT NEXT VALUE FOR Test.CountBy1 OVER (ORDER BY LastName) AS ListNumber,  
    FirstName, LastName  
FROM Person.Contact ;  
GO  

D. Using the NEXT VALUE FOR function in the definition of a default constraint

Using the NEXT VALUE FOR function in the definition of a default constraint is supported. For an example of using NEXT VALUE FOR in a CREATE TABLE statement, see Example CSequence Numbers. The following example uses ALTER TABLE to add a sequence as a default to a current table.

CREATE TABLE Test.MyTable  
(  
    IDColumn nvarchar(25) PRIMARY KEY,  
    name varchar(25) NOT NULL  
) ;  
GO  
  
CREATE SEQUENCE Test.CounterSeq  
    AS int  
    START WITH 1  
    INCREMENT BY 1 ;  
GO  
  
ALTER TABLE Test.MyTable  
    ADD   
        DEFAULT N'AdvWorks_' +   
        CAST(NEXT VALUE FOR Test.CounterSeq AS NVARCHAR(20))   
        FOR IDColumn;  
GO  
  
INSERT Test.MyTable (name)  
VALUES ('Larry') ;  
GO  
  
SELECT * FROM Test.MyTable;  
GO  

E. Using the NEXT VALUE FOR function in an INSERT statement

The following example creates a table named TestTable and then uses the NEXT VALUE FOR function to insert a row.

CREATE TABLE Test.TestTable  
     (CounterColumn int PRIMARY KEY,  
    Name nvarchar(25) NOT NULL) ;   
GO  
  
INSERT Test.TestTable (CounterColumn,Name)  
    VALUES (NEXT VALUE FOR Test.CountBy1, 'Syed') ;  
GO  
  
SELECT * FROM Test.TestTable;   
GO  
  

E. Using the NEXT VALUE FOR function with SELECT … INTO

The following example uses the SELECT … INTO statement to create a table named Production.NewLocation and uses the NEXT VALUE FOR function to number each row.

USE AdventureWorks2012 ;   
GO  
  
SELECT NEXT VALUE FOR Test.CountBy1 AS LocNumber, Name   
    INTO Production.NewLocation  
    FROM Production.Location ;  
GO  
  
SELECT * FROM Production.NewLocation ;  
GO  

F. Granting permission to execute NEXT VALUE FOR

The following example grants UPDATE permission to a user named AdventureWorks\Larry permission to execute NEXT VALUE FOR using the Test.CounterSeq sequence.

GRANT UPDATE ON OBJECT::Test.CounterSeq TO [AdventureWorks\Larry] ;  

See Also

CREATE SEQUENCE (Transact-SQL)
ALTER SEQUENCE (Transact-SQL)
Sequence Numbers