在SQL中使用Sequence Next Val来输入从序列到表行的所有值

时间:2019-07-17 06:22:34

标签: sql oracle

CREATE SEQUENCE pri_series_new
    START WITH 100000
    INCREMENT BY 1
    MINVALUE 000000
    MAXVALUE 99999999
    cycle

我需要此序列生成的所有值到表的行中。

INSERT into pri_temp VALUES(pri_series_new.nextval);

这一次只插入一个值,我需要插入所有值。

表中的行应包含序列对象正在生成的所有数字。

4 个答案:

答案 0 :(得分:2)

您实际上不需要序列,只需要一个行源代码...

insert 
  into pri_temp
select seq     
  from (select level as seq from dual connect by level <= 99999999)
 where seq >= 100000;

答案 1 :(得分:1)

我认为这应该对您有用:

DECLARE
    maxvalue number;
BEGIN
    select MAX_VALUE into maxvalue from all_sequences where sequence_name='PRI_SERIES_NEW';
    FOR i IN 1..maxvalue LOOP
        EXECUTE IMMEDIATE 'INSERT into pri_temp VALUES(PRI_SERIES_NEW.nextval)';
    END LOOP;

END;

答案 2 :(得分:0)

我认为您应该使用以下版本的SQL。

注意:我已经使用INCREMENT_BY处理序列的INCREMENT_BY不是1的情况。

INSERT INTO PRI_TEMP
    SELECT
        PRI_SERIES_NEW.NEXTVAL
    FROM
        (
            SELECT
                ( MAX_VALUE - LAST_NUMBER + INCREMENT_BY ) / INCREMENT_BY AS LOOP_CNT
            FROM
                USER_SEQUENCES
            WHERE
                SEQUENCE_NAME = 'PRI_SERIES_NEW'
        )
    CONNECT BY
        LEVEL <= LOOP_CNT;

db<>fiddle demo

干杯!

答案 3 :(得分:0)

您也可以这样做。

INSERT into pri_temp
select pri_series_new.nextval
  from dual
 connect by rownum <= (SELECT max_value FROM all_sequences WHERE lower(sequence_name) = 'pri_series_new')