Re: nextval() clarification

Поиск
Список
Период
Сортировка
От Michael Fuhr
Тема Re: nextval() clarification
Дата
Msg-id 20040913220226.GA55308@winnie.fuhr.org
обсуждение исходный текст
Ответ на Re: nextval() clarification  (Greg Donald <destiney@gmail.com>)
Список pgsql-novice
On Mon, Sep 13, 2004 at 03:55:41PM -0500, Greg Donald wrote:
> On Mon, 13 Sep 2004 21:54:11 +0200, Ennio-Sr <nasr.laili@tin.it> wrote:
> > I think you should put <nextval(....)> directly into the INSERT
> > instruction, so that it advances only once per added record.
> > Have a look at Documentation PostgreSQL 7.4, Ch 9.11, table 9.34.
> > HTH
>
> I would but then I don't know how to ensure I return that exact same
> id from the insert, like mysql_insert_id() does.  That's the whole
> reason why I aquire the id first, so I know what it is after the
> insert.  SELECT max(id) would be off under load I think.

Are you aware of CURRVAL()?

CREATE TABLE foo (
    id    SERIAL PRIMARY KEY,
    info  TEXT NOT NULL
);

INSERT INTO foo (info) VALUES ('First Item');
INSERT INTO foo (info) VALUES ('Second Item');
SELECT CURRVAL('foo_id_seq');

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

В списке pgsql-novice по дате отправления:

Предыдущее
От: Ron St-Pierre
Дата:
Сообщение: Re: nextval() clarification
Следующее
От: Mitch Pirtle
Дата:
Сообщение: Re: Opinions Requested - PG API or Abstraction Layer