INSERT using SEQUENCE . NEXTVAL with another SELECT

Asked

Viewed 23 times

0

I can insert with this query in ORACLE

INSERT INTO CONF_CODE (ID, OPTION_NAME, ISCHECKED) VALUES (SEQ_CONF_CODE.NEXTVAL, 'Producao', 0);

But I need to fill this table CONF_CODE with a SELECT from another table like this:

INSERT INTO CONF_CODE (OPTION_NAME, ISCHECKED)
SELECT DISPLAY_NAME, 0 FROM CONF_LABEL_LOCAL
WHERE "TYPE" = 2 ORDER BY ID;

I don’t include the ID in the first line even being a field not-null because I don’t know how to insert a NEXTVAL SEQUENCE with the SELECT down below.

I am using Dbeaver because my 'PL/SQL Developer' has expired

Any idea of solving?

1 answer

0


You can use Quence on select include the "ID" in the insert:

INSERT INTO CONF_CODE (ID, OPTION_NAME, ISCHECKED)
SELECT SOMEDASEQUENCE.NEXTVAL, DISPLAY_NAME, 0 
  FROM CONF_LABEL_LOCAL
 WHERE "TYPE" = 2 ORDER BY ID;
  • It gave an error in SEQUENCE: SQL Error [2287] [42000]: ORA-02287: sequence number not allowed here. I only have the properties of seq value:1, increment: 1 val_min: 0 val_max: 99999999. have pq idea?

  • I am reading the documentation where SEQUENCE cannot be used in sub-query

  • of course you can, look at this example I’ve put together now: http://sqlfiddle.com/#! 4/c5fe63/1 must be doing something wrong in the query or using a wrong query

  • yes. it worked. It was SEQUENCE, I remade it (silly thing!)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.