日期:2014-05-16  浏览次数:20513 次

zz修改序列的当前值
---修改序列的当前值
declare
  LastValue integer;
begin
  loop
    select seq_bl.currval into LastValue from dual;
    exit when LastValue >= 16000 - 1;
    select seq_bl.nextval into LastValue from dual;
  end loop;
end;
---------------------------------------
1.创建测试用序列S
sec@ora10g> drop sequence s;
sec@ora10g> create sequence s start with 99;

Sequence created.

2.使用seq简单查看一下s的基本信息
sec@ora10g> select * from seq where SEQUENCE_NAME = 'S';

SEQUENCE_NAME MIN_VALUE  MAX_VALUE INCREMENT_BY C O CACHE_SIZE LAST_NUMBER
------------- --------- ---------- ------------ - - ---------- -----------
S                     1 1.0000E+27            1 N N         20          99

3.使用dbms_metadata得到序列的详细的创建语句
sec@ora10g> set linesize 150
sec@ora10g> set longchunksize 1000
sec@ora10g> select dbms_metadata.get_ddl('SEQUENCE','S') as "Create Sequence Statements" from dual;

Create Sequence Statements
------------------------------------------------------------------------------

   CREATE SEQUENCE  "SEC"."S"  MINVALUE 1 MAXVALUE 999999999999999999999999999 INCREMENT BY 1 START WITH 99 CACHE 20 NOORDER  NOCYCLE

通过上面的创建,我们得到的序列s是从99开始的。

4.在对s取nextval时,推测一下,得到的值是多少??
重要结论一:在初创建的Sequence上第一次使用nextval的时候,得到是初始值,不是初始值加一!
请看下面的演示,得到的是初始化的99值,不是100!!
sec@ora10g> select s.nextval from dual;

   NEXTVAL
----------
        99

如果此时再继续取nextval的话,一切恢复正常
sec@ora10g> select s.nextval from dual;

   NEXTVAL
----------
       100

sec@ora10g> select s.nextval from dual;

   NEXTVAL
----------
       101

5.新开启的session中允许直接使用currval取Sequence的当前值么?
重要结论二:第一次NEXTVAL初始化之后才能使用CURRVAL取值。
sec@ora10g> conn sec/sec
Connected.
sec@ora10g> select s.currval from dual;
select s.currval from dual
       *
ERROR at line 1:
ORA-08002: sequence S.CURRVAL is not yet defined in this session

sec@ora10g> select s.nextval from dual;

   NEXTVAL
----------
       102

sec@ora10g> select s.currval from dual;

   CURRVAL
----------
       102

6.虽然上面看到currval必须在nextval之后使用,不过,可以在一条SQL语句中同时得到nextval和currval值,而且它们在SQL中不分先后顺序,请看下面的演示。
sec@ora10g> conn sec/sec
Connected.
sec@ora10g> select s.currval from dual;
select s.currval from dual
*
ERROR at line 1:
ORA-08002: sequence S.CURRVAL is not yet defined in this session


sec@ora10g> select s.currval, s.nextval from dual;

   CURRVAL    NEXTVAL
---------- ----------
       103        103

sec@ora10g> conn sec/sec
Connected.
sec@ora10g> select s.currval from dual;
select s.currval from dual
*
ERROR at line 1:
ORA-08002: sequence S.CURRVAL is not yet defined in this session


sec@ora10g> select s.nextval, s.currval from dual;

   NEXTVAL    CURRVAL
---------- ----------
       104        104

7.将Or