如何获得序列的当前值

heyongzhou 2001-11-24 10:31:57
select seq.currval from dual可以得到当前值,然而必须使用sqeq.nextval
之后

能不能直接得到currval???
...全文
454 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
KingSunSha 2001-11-24
  • 打赏
  • 举报
回复
IronPromises(铁诺)不是说了吗?在一个进程中,如果没有对序列取过nextval值,那是没法取currval的。
SQL> select con.currval from dual;
select con.currval from dual
*
ERROR at line 1:
ORA-08002: sequence CON.CURRVAL is not yet defined in this session


SQL> select con.nextval from dual;

NEXTVAL
----------
1

SQL> select con.currval from dual;

CURRVAL
----------
1
heyongzhou 2001-11-24
  • 打赏
  • 举报
回复
老大你看好!
SQL*Plus: Release 8.1.6.0.0 - Production on 星期六 11月 24 10:39:55 2001

(c) Copyright 1999 Oracle Corporation. All rights reserved.


连接到:
Oracle8i Enterprise Edition Release 8.1.6.0.0 - Production
With the Partitioning option
JServer Release 8.1.6.0.0 - Production

SQL> select con.currval from dual;
select con.currval from dual
*
ERROR 位于第 1 行:
ORA-08002: 序列CON.CURRVAL 尚未在此进程中定义


SQL>
IronPromises 2001-11-24
  • 打赏
  • 举报
回复

一开始序列是没有值的.
必须先用nextval初始化一下.
KittyWang 2001-11-24
  • 打赏
  • 举报
回复
什么意思???

使用nextval和currval没有关系的,如果想得到当前值使用currval就可以;
不需要使用nextval的呀

^_^^_^
heyongzhou 2001-11-24
  • 打赏
  • 举报
回复
好!
xinpingf 2001-11-24
  • 打赏
  • 举报
回复
不过有个问题,如果你的序列有cache的话,没用过时,取出来的是1
用过后就可能是n*cache+1了,
有cache的序列,会有很多数被浪费的,例:
create sequence con ;
select con.nextval from dual;

NEXTVAL
---------
1
此时如果重新启动数据库后再执行:
select con.nextval from dual;

NEXTVAL
---------
21
缺省的cache为20
所以一般来说,我创建的sequence都用这种方法:
create sequence con
start with 1
maxvalue 99999999
nocache;
xinpingf 2001-11-24
  • 打赏
  • 举报
回复
select last_number from user_sequences where sequence_name='CON';
heyongzhou 2001-11-24
  • 打赏
  • 举报
回复
真的没有别的办法吗?

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧