nextval

Select multiple ids from a PostgreSQL sequence

你离开我真会死。 提交于 2019-11-29 16:35:21
问题 Is there a concise way to select the nextval for a PostgreSQL sequence multiple times in 1 query? This would be the only value being returned. For example, I would like to do something really short and sweet like: SELECT NEXTVAL('mytable_seq', 3) AS id; And get: id ----- 118 119 120 (3 rows) 回答1: select nextval('mytable_seq') from generate_series(1,3); generate_series is a function which returns many rows with sequential numbers, configured by it's arguments. In above example, we don't care

Get default serial value after INSERT inside PL/pgSQL

烈酒焚心 提交于 2019-11-28 11:49:16
问题 I have a table. I wrote a function in plpgsql that inserts a row into this table: INSERT INTO simpleTalbe (name,money) values('momo',1000) ; This table has serial field called id . I want in the function after I insert the row to know the id that the new row received. I thought to use: select nextval('serial'); before the insert, is there a better solution? 回答1: Use the RETURNING clause. You need to save the result somewhere inside PL/pgSQL - with an appended INTO .. INSERT INTO simpleTalbe

PostgreSQL next value of the sequences?

我是研究僧i 提交于 2019-11-27 13:55:20
I am using PostgreSQL for my Codeigniter website. I am using grocery crud for add, edit and delete operations. While doing an edit or add, I want to rename an uploaded file dynamically based on the id of the content. I am able to do this using grocery crud's callback_after_upload function. I want a next id of the content while adding a new content. I tried to use nextval() function, but sequence gets incremented with it. How can get the last value of the sequence without using nextval() function? Or is there a simple way I can do this? Erwin Brandstetter RETURNING In modern-day PostgreSQL (8.2

get next sequence value from database using hibernate

点点圈 提交于 2019-11-26 19:56:20
I have an entity that has an NON-ID field that must be set from a sequence. Currently, I fetch for the first value of the sequence, store it on the client's side, and compute from that value. However, I'm looking for a "better" way of doing this. I have implemented a way to fetch the next sequence value: public Long getNextKey() { Query query = session.createSQLQuery( "select nextval('mySequence')" ); Long key = ((BigInteger) query.uniqueResult()).longValue(); return key; } However, this way reduces the performance significantly (creation of ~5000 objects gets slowed down by a factor of 3 -

get next sequence value from database using hibernate

纵饮孤独 提交于 2019-11-26 07:28:33
问题 I have an entity that has an NON-ID field that must be set from a sequence. Currently, I fetch for the first value of the sequence, store it on the client\'s side, and compute from that value. However, I\'m looking for a \"better\" way of doing this. I have implemented a way to fetch the next sequence value: public Long getNextKey() { Query query = session.createSQLQuery( \"select nextval(\'mySequence\')\" ); Long key = ((BigInteger) query.uniqueResult()).longValue(); return key; } However,