Lab-verifiedReproduced and fixed on Oracle AI Database 26ai Free 23.26.3.0.0 · 2026-11-29 · container image gvenzl/oracle-free:23-slim
Oracle allows NEXTVAL only in a few places: the SELECT list of a top-level query, the VALUES of an INSERT, or the SET of an UPDATE. In the same query as ORDER BY or DISTINCT, inside a subquery, or in a view definition it raises ORA-02287, and the * in the output points at the sequence.
The common fix is to move ORDER BY or DISTINCT into an inline view and call NEXTVAL outside it. Four cases verified on Oracle 26ai Free.
01INSERT ... SELECT seq.NEXTVAL ... ORDER BY
You want ids in code order while loading from a staging table, so you add ORDER BY to the INSERT SELECT. NEXTVAL cannot be used in a query with ORDER BY.
Setup
CREATE TABLE stage_items (code VARCHAR2(10), name VARCHAR2(30));
INSERT INTO stage_items VALUES ('B200','Bolt');
INSERT INTO stage_items VALUES ('A100','Nut');
CREATE TABLE items (id NUMBER PRIMARY KEY, code VARCHAR2(10), name VARCHAR2(30));
CREATE SEQUENCE item_seq;Table created.
1 row created.
1 row created.
Table created.
Sequence created.INSERT INTO items (id, code, name)
SELECT item_seq.NEXTVAL, code, name FROM stage_items ORDER BY code;SELECT item_seq.NEXTVAL, code, name FROM stage_items ORDER BY code
*
ERROR at line 2:
ORA-02287: sequence number not allowed here
Help: https://docs.oracle.com/error-help/db/ora-02287/SELECT code, name FROM stage_items ORDER BY code;CODE NAME
---------- ------------------------------
A100 Nut
B200 Bolt
2 rows selected.Put the ORDER BY in an inline view and take NEXTVAL outside: SELECT item_seq.NEXTVAL, code, name FROM (SELECT code, name FROM stage_items ORDER BY code). A100 gets id 1 and B200 gets id 2.
INSERT INTO items (id, code, name)
SELECT item_seq.NEXTVAL, code, name
FROM (SELECT code, name FROM stage_items ORDER BY code);
SELECT id, code, name FROM items ORDER BY id;2 rows created.
ID CODE NAME
---------- ---------- ------------------------------
1 A100 Nut
2 B200 Bolt
2 rows selected.02SELECT DISTINCT seq.NEXTVAL, ...
Building a department master from imported employees, SELECT DISTINCT dept_seq.NEXTVAL, dept_name is not allowed. NEXTVAL cannot be in a DISTINCT query.
Setup
CREATE TABLE emp_import (emp_name VARCHAR2(30), dept_name VARCHAR2(30));
INSERT INTO emp_import VALUES ('Kim','Sales');
INSERT INTO emp_import VALUES ('Lee','Sales');
INSERT INTO emp_import VALUES ('Park','IT');
CREATE TABLE depts (id NUMBER PRIMARY KEY, name VARCHAR2(30));
CREATE SEQUENCE dept_seq;Table created.
1 row created.
1 row created.
1 row created.
Table created.
Sequence created.INSERT INTO depts (id, name)
SELECT DISTINCT dept_seq.NEXTVAL, dept_name FROM emp_import;SELECT DISTINCT dept_seq.NEXTVAL, dept_name FROM emp_import
*
ERROR at line 2:
ORA-02287: sequence number not allowed here
Help: https://docs.oracle.com/error-help/db/ora-02287/SELECT DISTINCT dept_name FROM emp_import;DEPT_NAME
------------------------------
Sales
IT
2 rows selected.Do the DISTINCT first in an inline view: SELECT dept_seq.NEXTVAL, dept_name FROM (SELECT DISTINCT dept_name FROM emp_import). 2 rows are created: Sales 1, IT 2.
INSERT INTO depts (id, name)
SELECT dept_seq.NEXTVAL, dept_name
FROM (SELECT DISTINCT dept_name FROM emp_import);
SELECT id, name FROM depts ORDER BY id;2 rows created.
ID NAME
---------- ------------------------------
1 Sales
2 IT
2 rows selected.03UPDATE ... SET col = (SELECT seq.NEXTVAL FROM dual)
NEXTVAL in a subquery is not allowed, even a scalar subquery from dual. The * points at order_seq inside the subquery.
Setup
CREATE TABLE orders (order_no NUMBER, cust VARCHAR2(10));
INSERT INTO orders VALUES (NULL, 'A');
INSERT INTO orders VALUES (NULL, 'B');
COMMIT;
CREATE SEQUENCE order_seq START WITH 1000;Table created.
1 row created.
1 row created.
Commit complete.
Sequence created.UPDATE orders SET order_no = (SELECT order_seq.NEXTVAL FROM dual)
WHERE order_no IS NULL;UPDATE orders SET order_no = (SELECT order_seq.NEXTVAL FROM dual)
*
ERROR at line 1:
ORA-02287: sequence number not allowed here
Help: https://docs.oracle.com/error-help/db/ora-02287/SELECT order_no, cust FROM orders ORDER BY cust; ORDER_NO CUST
---------- ----------
A
B
2 rows selected.Use the sequence directly in SET: UPDATE orders SET order_no = order_seq.NEXTVAL WHERE order_no IS NULL. Each row gets its own number: 1000 and 1001.
UPDATE orders SET order_no = order_seq.NEXTVAL WHERE order_no IS NULL;
SELECT order_no, cust FROM orders ORDER BY cust;2 rows updated.
ORDER_NO CUST
---------- ----------
1000 A
1001 B
2 rows selected.04CREATE VIEW with NEXTVAL in the SELECT list
A view cannot contain NEXTVAL. The CREATE VIEW fails and the view is not created (user_objects returns no rows).
Setup
CREATE TABLE products (name VARCHAR2(30));
INSERT INTO products VALUES ('Pen');
COMMIT;
CREATE SEQUENCE label_seq;Table created.
1 row created.
Commit complete.
Sequence created.CREATE VIEW v_labels AS SELECT label_seq.NEXTVAL AS label_no, name FROM products;CREATE VIEW v_labels AS SELECT label_seq.NEXTVAL AS label_no, name FROM products
*
ERROR at line 1:
ORA-02287: sequence number not allowed here
Help: https://docs.oracle.com/error-help/db/ora-02287/SELECT object_name, object_type FROM user_objects WHERE object_name = 'V_LABELS';no rows selectedCreate the view without the sequence and call NEXTVAL in the query that reads the view: SELECT label_seq.NEXTVAL AS label_no, name FROM v_labels.
CREATE VIEW v_labels AS SELECT name FROM products;
SELECT label_seq.NEXTVAL AS label_no, name FROM v_labels;View created.
LABEL_NO NAME
---------- ------------------------------
1 Pen
1 row selected.FAQ
How do I use NEXTVAL with ORDER BY in Oracle?
Put ORDER BY in an inline view and select NEXTVAL from it: SELECT seq.NEXTVAL, x FROM (SELECT x FROM t ORDER BY x).
Can I use NEXTVAL in a subquery?
No. Use seq.NEXTVAL directly in the INSERT VALUES, the top-level SELECT list or the UPDATE SET.
Can a view use a sequence?
Not inside the view definition. Call NEXTVAL in the query that selects from the view.
How do I insert distinct values with a sequence id?
INSERT INTO t (id, v) SELECT seq.NEXTVAL, v FROM (SELECT DISTINCT v FROM src);
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary users created for the test; system is the DBA account. Oracle AI Database 26ai Free Release 23.26.3.0.0 - Develop, Learn, and Run for Free 23.26.3.0.0