选择/显示postgres中最后插入的序列号 [英] select/display last inserted serial id in postgres
本文介绍了选择/显示postgres中最后插入的序列号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在sql server中,我这样做:
in sql server I do like this:
insert into foo(name) values('bob')
select @@identity;
所以我得到显示的查询/标量结果
so I get a query/scalar result displayed
如何使用postgres完成此操作?
how to this with postgres ?
推荐答案
获取特定序列:
SELECT currval('name_of_your_sequence');
从上次使用的序列中获取最后一个值:
Get the last value from the last sequence used:
SELECT lastval();
还要检查手册: http://www.postgresql.org/docs/current/static/functions-sequence.html
编辑:您也可以在INSERT中使用RETURNING:
You could also use RETURNING in your INSERT:
INSERT INTO foo(id, name) VALUES(DEFAULT, 'bob') RETURNING id;
这篇关于选择/显示postgres中最后插入的序列号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文