oracle 插入一个序列并选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6628923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 00:07:54  来源:igfitidea点击:

Insert a sequence and SELECT

sqloracleplsqlinsertsequence

提问by null

I'm trying to insert into a table, where the ID is the next in a sequence, and some of the other values are pulled from a table. I know that this won't work:

我正在尝试插入一个表,其中 ID 是序列中的下一个,而其他一些值是从表中提取的。我知道这行不通:

INSERT INTO ORDERS(order_id, foo1, foo2, foo3
VALUES(SEQUENCE_ORDERS.nextval,(SELECT foo1, foo2, foo3
                                FROM bar
                                WHERE X = Y))

I just tried, and it I get a "not enough values" error.

我刚刚尝试过,但出现“值不足”错误。

I wanted to know what the correct syntax was to get this to work. I'm using PLSQL in an Oracle DB (part of a procedure I'm writing).

我想知道正确的语法是什么才能让它工作。我在 Oracle DB 中使用 PLSQL(我正在编写的过程的一部分)。

I know that the select should not be inside the VALUES() but how do I also add the sequence? The only option I can think of would be a trigger on the insert, but I'd rather not have to do that.

我知道选择不应该在 VALUES() 内,但我该如何添加序列?我能想到的唯一选择是插入触发器,但我宁愿不必这样做。

EDIT: thank you all, just as I figured it out, everyone posted. Didn't know it was ok to have the sequence in the select.

编辑:谢谢大家,就像我想出来的那样,每个人都发布了。不知道在选择中有序列是可以的。

回答by dcp

INSERT INTO ORDERS(order_id, foo1, foo2, foo3)
SELECT SEQUENCE_ORDERS.nextval,foo1, foo2, foo3
                                FROM bar
                                WHERE X = Y

回答by Cos Callis

Try:

尝试:

INSERT INTO ORDERS(order_id, foo1, foo2, foo3
SELECT SEQUENCE_ORDERS.nextval, foo1, foo2, foo3
                                FROM bar
                                WHERE X = Y

回答by Lost in Alabama

Try moving the SEQUENCE over to the SELECT statement.

尝试将 SEQUENCE 移至 SELECT 语句。