[英]Can't insert multiple values into DB2 by using UNION ALL and generate IDs from sequence
I've created sequence by following statement: 我通过以下语句创建了序列:
CREATE SEQUENCE MAIN.MY_SEQUENCE START WITH 1 INCREMENT BY 1 CACHE 50;
And table by following statement: 并通过以下语句表:
CREATE TABLE MAIN.EMPLOYEES(
ID INTEGER NOT NULL,
NAME VARCHAR(512),
EMAIL VARCHAR(254),
PRIMARY KEY (ID)
)
Now when I try to insert a new record by using following statement: 现在,当我尝试使用以下语句插入新记录时:
INSERT INTO MAIN EMPLOYEES (ID, NAME, EMAIL)
VALUES (MAIN.MY_SEQUENCE.NEXTVAL, 'Name 1', 'email1@example.com') UNION ALL
VALUES (MAIN.MY_SEQUENCE.NEXTVAL, 'Name 2', 'email2@example.com')
I get an error: 我收到一个错误:
"NEXTVAL FOR MAIN.MY_SEQUENCE.NEXTVAL" cannot be specified in this context.. SQLCODE=-348, SQLSTATE=428F9, DRIVER=4.17.30
When I try to insert a single row everything works fine. 当我尝试插入一行时,一切正常。
I have found a list of restrictions on using NEXT VALUE
here but here not mentioned my case or I couldn't find it. 我发现使用的限制列表
NEXT VALUE
这里 ,但这里没有提到我的情况还是我找不到它。
My question is it possible to insert multiple rows by using ID from sequence, and if yes, how can I achieve it? 我的问题是可以通过使用序列中的ID插入多行吗,如果可以,如何实现?
It does list your case. 它确实列出了您的情况。 The documentation contains this:
该文档包含以下内容:
The NEXT VALUE expressions cannot be specified in the following contexts:
在以下情况下不能指定NEXT VALUE表达式:
......
•SELECT statement for which the outer SELECT is combined with another SELECT statement using a set operator such as UNION, EXCEPT, or INTERSECT•使用集合运算符( 例如 UNION,EXCEPT或INTERSECT)将外部SELECT与另一个SELECT语句组合的SELECT语句
........
(emphasis mine) This statement isn't exhaustive, and because UNION ALL
is considered a set operation, the operation is excluded. (强调我的)此语句不是穷举性的,并且因为
UNION ALL
被视为设置操作,所以该操作被排除在外。
This should be fixable - I'm a little surprised you wrote the statement the way you did; 这应该是可以解决的-我有点惊讶您以这种方式编写了该声明; DB2 allows you to comma-separate data rows.
DB2允许您用逗号分隔数据行。 That is, the following should be valid:
也就是说,以下内容应有效:
INSERT INTO MAIN.EMPLOYEES (ID, NAME, EMAIL)
VALUES (MAIN.MY_SEQUENCE.NEXTVAL, 'Name 1', 'email1@example.com'),
(MAIN.MY_SEQUENCE.NEXTVAL, 'Name 2', 'email2@example.com')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.