9

我创建了一个表和序列以替换表中的标识我使用 SQL Server 2012 Express 但在尝试将数据插入表时出现此错误

消息 11719,级别 15,状态 1,第 2 行
NEXT VALUE FOR 函数在检查约束、默认对象、计算列、视图、用户定义函数、用户定义聚合、用户定义表类型、子查询中不允许使用,公用表表达式或派生表。

T-SQL 代码:

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

这是我的桌子:

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)
);

这是我的顺序:

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

3 回答 3

18

只需去掉 VALUES 部分中的子选择,如下所示:

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

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

于 2012-10-05T11:20:20.980 回答
7

您的插入语法似乎是错误的。您正在尝试在查询部分中使用SELECT语句。VALUES如果你想使用,SELECT那么你将使用:

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

请参阅带有演示的 SQL Fiddle

我将语法从 更改INSERT INTO VALUESINSERT INTO ... SELECT。我使用它是因为您正在选择序列的下一个值。

但是,如果要使用INSERT INTO.. VALUES,则必须SELECT从查询中删除 :

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

请参阅带有演示的 SQL Fiddle

这两个都将INSERT记录放入表中。

于 2012-10-05T11:16:33.553 回答
0

试试这个:


– 带桌子

创建序列 idsequence 以 1 递增 3 开始

create table Products_ext
(
id int,
Name varchar(50)
);

INSERT dbo.Products_ext (Id, Name)
VALUES (NEXT VALUE FOR dbo.idsequence, ‘ProductItem’);

select * from Products_ext;


/* If you run the above statement two types, you will get the following:-

1    ProductItem
4    ProductItem

*/

drop table Products_ext;
drop sequence idsequence;

------------------------------
于 2012-11-16T19:59:39.260 回答