Skip Navigation Links
Home
Articles
SQL Server 2012
SQL Server 2014
SQL Server 2016
FAQ
Forums
Practice Test
Bookstore
Tip of the Day : 2 Ways to Execute Operating System Commands From SQL Server Management Studio
Error Messages
Home > SQL Server Error Messages > Msg 11719 - NEXT VALUE FOR function is not allowed in check constraints, default objects, computed columns, views, user-defined functions, user-defined aggregates, user-defined table types, sub-queries, common table expressions, or derived tables.
SQL Server Error Messages - Msg 11719 - NEXT VALUE FOR function is not allowed in check constraints, default objects, computed columns, views, user-defined functions, user-defined aggregates, user-defined table types, sub-queries, common table expressions, or derived tables.

Error Message

Server: Msg 11719, Level 15, State 1, Line 1
NEXT VALUE FOR function is not allowed in check
constraints, default objects, computed columns,
views, user-defined functions, user-defined aggregates,
user-defined table types, sub-queries, common table
expressions, or derived tables.

Causes

A sequence is a user-defined schema-bound object that generates a sequence of numeric values according to the specification with which the sequence object was created.

The sequence of numeric values is generated in an ascending or descending order at a defined interval and may cycle or repeat as requested. Sequences, unlike identity columns, are not associated with tables.

A sequence is created independently of table objects by using the CREATE SEQUENCE statement. Options enable you to control the increment, maximum and minimum values, starting point, automatic restarting capability, and caching to improve performance.

The syntax of the CREATE SEQUENCE statement is as follows:

CREATE SEQUENCE <Sequence Name> AS <integer_type>
 [ START WITH <constant> ]
 [ INCREMENT BY <constant> ]
 [ { MINVALUE [ <constant> ] } | { NO MINVALUE } ]
 [ { MAXVALUE [ <constant> ] } | { NO MAXVALUE } ]
 [ CYCLE | { NO CYCLE } ]
 [ { CACHE [ <constant> ] } | { NO CACHE } ]

To generate a sequence number from a specified sequence object, the NEXT VALUE FOR metadata function is used. Unlike identity column values, which are generated when rows are inserted, an application can obtain the next sequence number before inserting the row by calling the NEXT VALUE FOR function. The sequence number is allocated when NEXT VALUE FOR is called even if the number is never inserted into a table.

The NEXT VALUE FOR function is a non-deterministic function, and is only allowed in contexts where the number of generated sequence values is well defined. There are a lot of limitations and restrictions with the use of the NEXT VALUE FOR. The NEXT VALUE FOR is not allowed to be used in views, in user-defined functions, or in computed columns. Including it in a view definition will raise this error message.

Here’s a script that shows the NEXT VALUE FOR used in a view definition and therefore generating this error message:

CREATE SEQUENCE [dbo].[UserSequence] AS INT
START WITH 1
GO

CREATE VIEW [dbo].[UserSequenceView]
AS
SELECT NEXT VALUE FOR [dbo].[UserSequence] AS [UserSequence]
GO
Msg 11719, Level 15, State 1, Procedure UserSequenceView, Line 3
NEXT VALUE FOR function is not allowed in check constraints, default objects, computed columns, views,
user-defined functions, user-defined aggregates, user-defined table types, sub-queries, common table
expressions, or derived tables.

Solution / Work Around:

To overcome the limitation of the use of NEXT VALUE FOR in view definitions, the OPENROWSET rowset function can be used. The OPENROWSET is an alternative to accessing tables in a linked server, or in this case, the local server, and is a one-time, ad hoc method of connecting and accessing remote data by using OLE DB.

Here’s how the statement will look like that indirectly uses the NEXT VALUE FOR statement in a view definition overcoming the limitation with the help of the OPENROWSET rowset function:

CREATE VIEW [dbo].[UserSequenceView]
AS
SELECT *
FROM OPENROWSET('SQLNCLI', 'Server=.\SQL2012;Database=SQL2012;UID=gis;Pwd=g1s4pp', 
                'SELECT NEXT VALUE FOR [dbo].[UserSequence] AS [UserSequence]')
GO

Here is a sample output from this view:

SELECT * FROM [dbo].[UserSequenceView]
UserSequence
-------------
1
Related Articles :