Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequence error in sql. Sequence number not allowed here

I am trying to run the following query to insert a number of nodes with an id that auto-increments as nodes are loaded into the table.

However I get the error, ORA-02287: sequence number not allowed here whenever I run it.

INSERT INTO V1144Engine.T_NODES VALUES
  (
    (SELECT V1144ENGINE.S_PK_NODES.NEXTVAL FROM dual),
    1,
    'Chemistry of Life',
    0,1,
    SYSDATE,
    NULL,
    'CON.3.1',
    NULL
  );

I have tried running

SELECT V1144ENGINE.S_PK_NODES.NEXTVAL from dual

This works fine and returns the number that I want.

How do I get around this? I am running on Oracle 11g.

Also it would be much appreciated if the query were still runnable on one line as I am making these in a spreadsheet and would like to still be able to do so.

like image 876
Slater Victoroff Avatar asked Jun 29 '12 18:06

Slater Victoroff


People also ask

What is seq Nextval in SQL?

The Oracle NEXTVAL function is used to retrieve the next value in a sequence. The Oracle NEXTVAL function must be called before calling the CURRVAL function, or an error will be thrown.


1 Answers

There is no need to have the inner SELECT. Simply

INSERT INTO V1144Engine.T_NODES 
  VALUES(V1144ENGINE.S_PK_NODES.NEXTVAL,
         1,
         'Chemistry of Life',
         0,
         1,
         SYSDATE,
         null,
         'CON.3.1',
         null);

In general, though, you want to list the columns that you are providing values for in your INSERT statement. That not only documents the columns so that a future developer doesn't have to look up the order of columns in a table, it protects you if new columns are added to the table in the future.

INSERT INTO V1144Engine.T_NODES( <<list of columns>> ) 
  VALUES(V1144ENGINE.S_PK_NODES.NEXTVAL,
         1,
         'Chemistry of Life',
         0,
         1,
         SYSDATE,
         null,
         'CON.3.1',
         null);
like image 80
Justin Cave Avatar answered Oct 23 '22 08:10

Justin Cave