ORA-02287: sequence number not allowed here

爱⌒轻易说出口 提交于 2019-12-12 01:45:43

问题


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.

  • In a WHERE clause
  • In a GROUP BY or ORDER BY clause
  • In a DISTINCT clause
  • Along with a UNION or INTERSECT or MINUS
  • In a sub-query

    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

  • 易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
    该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!