问题
I trying to insert data from one table to other table and using a sequence while but I'm getting the sequence not allowed here error on Oracle 12c.
INSERT INTO table_a
(a,b,c,d)
SELECT
schema.table_a_seq.nextval,
t.col1,
t.col2,
t.col3
FROM
(
SELECT col1, col2, col3 FROM table_b
)t;
回答1:
I couldn't reproduce the error, However I have changed your query to-
INSERT INTO table_a
SELECT table_a_seq.nextval, t.*
FROM (SELECT col1 FROM table_b) t;
And its working fine for me. You might have missed some part of the query to include here in the question.
The ORA-02287
can be raised if you used the sequence in the following part of SELECT
statement.
WHERE
clause
GROUP BY
or ORDER BY
clause
DISTINCT
clause
UNION
or INTERSECT
or MINUS
Reference:OraFAQ:ORA-02287
SQL> desc table_a
Name Null? Type
----------------------------------------- -------- ----------------------------
COL1 NUMBER
COL2 NUMBER
SQL> desc table_b
Name Null? Type
----------------------------------------- -------- ----------------------------
COL1 NUMBER
COL2 NUMBER
SQL> select * from table_a;
no rows selected
SQL> select * from table_b;
COL1 COL2
---------- ----------
2 3
4 5
SQL> INSERT INTO table_a SELECT table_a_seq.nextval, t.* FROM (SELECT col1 FROM table_b) t;
2 rows created.
SQL> select * from table_a;
COL1 COL2
---------- ----------
3 2
4 4
回答2:
I didn't see the error When I tried to use the schema name before every object in the SQL query.
INSERT INTO schema.table_a
(a,b,c,d)
SELECT
schema.table_a_seq.nextval,
t.col1,
t.col2,
t.col3
FROM
(
SELECT col1, col2, col3 FROM schema.table_b
)t;
来源:https://stackoverflow.com/questions/41155090/ora-02287-sequence-number-not-allowed-here