ORA-02287: 此处不允许序号

时间:2022-11-25 21:27:47

今天使用 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 < ''
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 < ''
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/