简体   繁体   English

如何在Oracle中多行插入序列?

[英]how to multi-row insert in Oracle with sequence?

I want insert more than one row into table in Oracle . 我想在Oracle中的表中插入多行。 But it's confused me : I can not use 但它让我困惑:我无法使用

select seq_table1.nextval  into table1 form dual

because the the table1 need be a new one. 因为table1需要一个新的。 I need insert some rows into the exist table1. 我需要在存在的table1中插入一些行。

And I also can not use 我也无法使用

insert into pager (PAG_ID,PAG_PARENT,PAG_NAME,PAG_ACTIVE)
          (select seq_paper.nextval,'Multi 8000',1 from dual
 union all select seq_paper.nextval,'Multi 8001',1 from dual)

because oracle tell me that: 因为oracle告诉我:

Restrictions on Sequence Values You cannot use CURRVAL and NEXTVAL in the  
following constructs:  
■ A SELECT statement that is combined with another SELECT statement with the  
UNION, INTERSECT, or MINUS set operator ;
...and other constructs

You can rephrase the select so that the nextval is not in a union . 您可以重新select以便nextval不在union

The result would be something like: 结果将是这样的:

insert into pager (PAG_ID,PAG_PARENT,PAG_NAME,PAG_ACTIVE) 
    select seq_paper.nextval, NULL, a, b from 
        (select 'Multi 8000' a, 1 b from dual 
         union all 
         select seq_paper.nextval,'Multi 8001',1 from dual)

Use nextval once and create data inside sub query: 使用nextval一次并在子查询中创建数据:

SQL> CREATE TABLE pager (PAG_ID NUMBER,PAG_PARENT VARCHAR2(10), PAG_ACTIVE NUMBER);
Table created

SQL> CREATE SEQUENCE seq_paper START WITH 1 INCREMENT BY 1 MINVALUE 1 NOMAXVALUE;
Sequence created

SQL> 

SQL> INSERT INTO pager
  2      (pag_id,
  3       pag_parent,
  4       pag_active)
  5      SELECT seq_paper.nextval,
  6             pag_parent,
  7             pag_active
  8        FROM (SELECT 'Multi 8000' pag_parent,
  9                     1 pag_active
 10                FROM dual
 11              UNION ALL
 12              SELECT 'Multi 8001',
 13                     1
 14                FROM dual);
2 rows inserted

SQL> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM