ORA-02287: 此處不允許序號


今天使用 insert into select 時出現了這個異常,感覺很詭異,去metalink查了下資料,找出了錯誤原因,記錄下來。

SQL> CREATE TABLE test_baser01(
  2                ID        NUMBER NOT NULL,
  3                NAME      VARCHAR2(50),
  4                age       NUMBER,
  5                sex       NUMBER,
  6                comments  VARCHAR2(240)
  7                );
表已創建。
SQL> CREATE SEQUENCE test_baser01_s;
序列已創建。
SQL> INSERT INTO Test_Baser01
  2      (Id, NAME)
  3      SELECT Test_Baser01_s.Nextval, A1
  4        FROM Temp_a
  5       WHERE A1 < '32000010'
  6       ORDER BY A1;
    SELECT Test_Baser01_s.Nextval, A1
                          *
ERROR 位於第3行:
ORA-02287: 此處不允許序號

資料:

Restrictions on Sequence Values You cannot use CURRVAL and NEXTVAL in the
following constructs:
■ A subquery in a DELETE, SELECT, or UPDATE statement
■ A query of a view or of a materialized view
■ A SELECT statement with the DISTINCT operator
■ A SELECT statement with a GROUP BY clause or ORDER BY clause
■ A SELECT statement that is combined with another SELECT statement with the
UNION, INTERSECT, or MINUS set operator
■ The WHERE clause of a SELECT statement
■ The DEFAULT value of a column in a CREATE TABLE or ALTER TABLE statement
■ The condition of a CHECK constrain

解決方案:

sql外面包一層

SQL> INSERT INTO Test_Baser01
  2      (Id, NAME)
  3      SELECT Test_Baser01_s.Nextval, A1
  4        FROM (SELECT *
  5                FROM Temp_a
  6               WHERE A1 < '32000010'
  7               ORDER BY A1);
已創建6行。
SQL> SELECT Id, NAME FROM Test_Baser01;
       ID NAME
--------- --------------------------------------------------
        1 32000004
        2 32000005
        3 32000006
        4 32000007
        5 32000008
        6 32000009
已選擇6行。
SQL>

from:http://blog.itpub.net/23009281/viewspace-766076/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM