当前位置: 代码迷 >> Oracle技术 >> ORA-02287: sequence number not allowed here
  详细解决方案

ORA-02287: sequence number not allowed here

热度:897   发布时间:2016-04-24 08:31:26.0
oracle中,我做了个多条数据插入报错了 ORA-02287: sequence number not allowed here
sql语句如下:
insert into t_fms_dictionary(id,dic_key,dic_lang,DIC_UPDATABLE,DIC_VALUE)  
select seq_dictionary_id.nextval, '1','1' ,'1' ,'1' from dual 
union all 
select seq_dictionary_id.nextval , '2','2','2','2' from dual 
union all 
select seq_dictionary_id.nextval , '3','3','3','3' from dual

序列:
DROP SEQUENCE QBET.SEQ_DICTIONARY_ID;

CREATE SEQUENCE QBET.SEQ_DICTIONARY_ID
  START WITH 11161
  MAXVALUE 9999999999999999999999999999
  MINVALUE 1
  NOCYCLE
  CACHE 20
  NOORDER;

------解决方案--------------------
不能像你那样。只能一个插入一个查询。
------解决方案--------------------
序列不能这样弄,你想要什么效果,考虑换个方式
------解决方案--------------------
普通的Insert into 语句一次只能插入一条记录
楼主如果要想一段sql 插多条记录,可以用For循环,在循环中,变量定义为lp
insert into t_fms_dictionary(id,dic_key,dic_lang,DIC_UPDATABLE,DIC_VALUE)
value (seq_dictionary_id.nextval, lp ,lp ,lp ,lp) --不需要select
如果是想一次插入多条记录,只执行一次插入操作,可以用Forall
Type a_type is record 
(id number,dic_key varchar2,dic_lang varchar2 ,DIC_UPDATABLE varchar2,DIC_VALUE varchar2);
Type a_table is table of a_type index by BINARY_INTEGER; 
declare
a a_table;
begin
For i in 1..n loop --循环赋值
 a[i].id := seq_dictionary_id.nextval;
 a[i].dic_key:= '';
.....
end loop;
FORALL i IN a.first .. a.last 一次性插入
INSERT INTO bs_contract_lotitem VALUES a(i);
end ;
------解决方案--------------------
oracle不让这么用。说明如下:
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 code
create or replace function get_seq (p_in_sqname in varchar2) return numberis  l_res number ;begin  execute immediate 'select '|| p_in_sqname|| '.nextval from dual' into l_res ;  return l_res ;end ;
------解决方案--------------------
請分段執行
------解决方案--------------------
可以试一试,insert into t_fms_dictionary(id,dic_key,dic_lang,DIC_UPDATABLE,DIC_VALUE) 
select get_seq('seq_dictionary_id'),A.* from (
select '1','1' ,'1' ,'1' from dual
union all
select '2','2','2','2' from dual
union all
select '3','3','3','3' from dual) A 
  相关解决方案