插入序列并选择
我正在尝试插入到一个表中,其中 ID 是序列中的下一个,并且从表中提取一些其他值。我知道这行不通:
INSERT INTO ORDERS(order_id, foo1, foo2, foo3
VALUES(SEQUENCE_ORDERS.nextval,(SELECT foo1, foo2, foo3
FROM bar
WHERE X = Y))
我刚刚尝试过,但出现“值不足”错误。
我想知道让它工作的正确语法是什么。我在 Oracle DB 中使用 PLSQL(我正在编写的过程的一部分)。
我知道选择不应位于 VALUES() 内部,但如何添加序列?我能想到的唯一选择是插入物上的触发器,但我宁愿不必这样做。
编辑:谢谢大家,正如我所想的那样,每个人都发帖了。不知道在选择中包含序列是否可以。
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:
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).
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试:
Try:
尝试将 SEQUENCE 移至 SELECT 语句。
Try moving the SEQUENCE over to the SELECT statement.
插入 DON AUDITI(DON SEO_ID.NEXTVAL、I OLDCUSTED、I MENCUSTED)
值('T_CRED NEXUS _CUST ACCESS',I.PEOPLEVISE_ID,I_CREATEDBY,SYSDATE);
INSERT into DON AUDITI (DON SEO_ID. NEXTVAL, I OLDCUSTED, I MENCUSTED)
values ('T_CRED NEXUS _CUST ACCESS' , I. PEOPLEVISE_ID, I_CREATEDBY, SYSDATE);