SQL Server 2012序列
为了替换表中的标识,我创建了一个表和序列。我使用SQL Server 2012 Express,但在尝试向表中插入数据时出现了此错误 Msg 11719,第15级,状态1,第2行SQL Server 2012序列,sql,sql-server,tsql,sequences,sql-server-2012-express,Sql,Sql Server,Tsql,Sequences,Sql Server 2012 Express,为了替换表中的标识,我创建了一个表和序列。我使用SQL Server 2012 Express,但在尝试向表中插入数据时出现了此错误 Msg 11719,第15级,状态1,第2行 在检查约束、默认对象、计算列、, 视图、用户定义函数、用户定义聚合、用户定义 表类型、子查询、通用表表达式或派生 桌子 T-SQL代码: insert into Job_Update_Log(log_id, update_reason, jobid) values((select next value for Job
在检查约束、默认对象、计算列、, 视图、用户定义函数、用户定义聚合、用户定义 表类型、子查询、通用表表达式或派生 桌子 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
您的插入语法似乎错误。您正试图在查询的
VALUES
部分中使用SELECT
语句。如果要使用选择,则将使用:
insert into Job_Update_Log(log_id,update_reason,jobid)
select next value for Job_Log_Update_SEQ,'grammer fixing',39;
看
我将语法从INSERT-INTO-VALUES
更改为INSERT-INTO。。。选择
。我使用它是因为您正在选择序列的下一个值
但是,如果要使用插入。。值,则必须从查询中删除选择:
insert into Job_Update_Log(log_id,update_reason,jobid)
values(next value for Job_Log_Update_SEQ,'grammer fixing',39);
看
这两种方法都将将记录插入表中 只需去掉VALUES部分中的subselect,如下所示:
insert into Job_Update_Log(log_id,update_reason,jobid)
values (next value for Job_Log_Update_SEQ,'grammer fixing',39);
参考资料:试试这个:
–有一张桌子
创建序列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;
------------------------------