currval()
是一个返回序列当前值的系统函数。
currval()
在 PostgreSQL 6.1 中被添加。
用法
currval (regclass
) →bigint
currval()
返回当前会话中,此序列最近一次被 nextval()
获取的值。
如果当前会话尚未为该序列执行 nextval()
,则会引发错误。
变更历史
- PostgreSQL 6.1
- 添加 (提交 e8647c45)
示例
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)
参考资料
- PostgreSQL 文档: 序列函数