currval()

返回序列当前值的函数

currval() 是一个返回序列当前值的系统函数。

currval()PostgreSQL 6.1 中被添加。

用法

currval ( regclass ) → bigint

currval() 返回当前会话中,此序列最近一次被 nextval() 获取的值。

如果当前会话尚未为该序列执行 nextval(),则会引发错误。

变更历史

示例

currval() 的基本用法示例

postgres=# CREATE SEQUENCE foo_seq;
CREATE SEQUENCE

postgres=# SELECT nextval('foo_seq');
 nextval
---------
       1
(1 row)

postgres=# SELECT currval('foo_seq');
 currval
---------
       1
(1 row)

如果在当前会话中未调用 nextval(),则会引发错误

postgres=# SELECT currval('foo_seq');
ERROR:  currval of sequence "foo_seq" is not yet defined in this session

postgres=# SELECT nextval('foo_seq');
 nextval
---------
       2
(1 row)

postgres=# SELECT currval('foo_seq');
 currval
---------
       2
(1 row)

分类

序列, 系统函数

另请参阅

lastval(), nextval(), setval()

反馈

提交有关“currval()”的任何评论、建议或更正,请点击 此处