sqloracleplsqlinsertsequence

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
                                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.


Solution

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