SQL Server 2012 sequence

danarj picture danarj · Oct 5, 2012 · Viewed 7.9k times · Source

I create a table and sequence in order to replace identity in the table I use SQL Server 2012 Express but I get this error while I tried to insert data to the table

Msg 11719, Level 15, State 1, Line 2
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.

T-SQL code:

insert into Job_Update_Log(log_id, update_reason, jobid) 
values((select next value for Job_Log_Update_SEQ),'grammer fixing',39);

This is my table:

create table Job_Update_Log
(
   log_id int primary key  ,
   update_reason nvarchar(100) ,
   update_date date default getdate(),
   jobid bigint not null,
   foreign key(jobid) references jobslist(jobid)
);

and this is my sequence:

CREATE SEQUENCE [dbo].[Job_Log_Update_SEQ] 
 AS [int]
 START WITH 1
 INCREMENT BY 1
 NO CACHE 
GO

Answer

TToni picture TToni · Oct 5, 2012

Just get rid of the subselect in the VALUES section, like this:

insert into Job_Update_Log(log_id,update_reason,jobid) 
        values (next value for Job_Log_Update_SEQ,'grammer fixing',39);

Reference: http://msdn.microsoft.com/en-us/library/hh272694%28v=vs.103%29.aspx