select sequence.currval 時報錯 ORA-08002: 序列XXXX尚未在此進程中定義,sequencecurrval

來源:互聯網
上載者:User

select sequence.currval 時報錯 ORA-08002: 序列XXXX尚未在此進程中定義,sequencecurrval

情景描述:

1.我們建立一個表,表名為S_DEPART

-- Create tablecreate table S_DEPART(  departid    INTEGER not null,  departname  NVARCHAR2(40) not null,  departorder INTEGER default 0)
2.建立一個sequence序列,名稱為S_DEPART_S

CREATE SEQUENCE S_DEPART_S MINVALUE 1MAXVALUE 9999999999999999999999START WITH 1INCREMENT BY 1NOCACHE;
3.下面我們往該表裡插入一條記錄

insert into S_Depart(departid,Departname,Departorder)VALUES(S_DEPART_S.CURRVAL,'12345',1);
此時,出現如下錯誤:

報錯  ORA-08002: 序列XXXX尚未在此進程中定義

錯誤分析:sequence序列的CURRVAL必須要先用NEXTVAL初始化一下,以後就可以用CURRVAL了。

解決辦法:先用NEXTVAL初始化:

<pre name="code" class="sql">insert into S_Depart(departid,Departname,Departorder)VALUES(S_DEPART_S.NEXTVAL,'12345',1);

此時我們可以查詢sequence序列S_Depart_s的值:

SELECT S_DEPART_S.CURRVAL FROM dual;
從我們可以看到S_Depart_s序列的當前值為2


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.