开发者

Insert a sequence and SELECT

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
开发者_如何学C                                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.


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


Try:

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


Try moving the SEQUENCE over to the SELECT statement.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜