Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL Server 2012序列_Sql_Sql Server_Tsql_Sequences_Sql Server 2012 Express - Fatal编程技术网

SQL Server 2012序列

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

为了替换表中的标识,我创建了一个表和序列。我使用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_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;

------------------------------