Re: how to get id of last insert on a serial type?

Поиск
Список
Период
Сортировка
От Joel Burton
Тема Re: how to get id of last insert on a serial type?
Дата
Msg-id JGEPJNMCKODMDHGOBKDNKELLCOAA.joel@joelburton.com
обсуждение исходный текст
Ответ на Re: how to get id of last insert on a serial type?  ("Robert J. Sanford, Jr." <rsanford@trefs.com>)
Список pgsql-general
> -----Original Message-----
> From: pgsql-general-owner@postgresql.org
> [mailto:pgsql-general-owner@postgresql.org]On Behalf Of Robert J.
> Sanford, Jr.
> Sent: Sunday, May 19, 2002 11:33 AM
> To: pgsql-general@postgresql.org
> Subject: Re: [GENERAL] how to get id of last insert on a serial type?
>
>
> Two quick notes/questions...
>
> 1) That would still require me to write a function for each of
>    inserts to make sure that the value came back. I can't just
>    do a "SELECT @@IDENTITY" and get back the last identity that
>    was inserted, I have to know what sequence was used. The
>    current code base that is returning the @@IDENTITY doesn't
>    know anything about what was actually inserted, just that
>    something was.

Am I understanding correctly -- You're getting the PK but you don't
know/care which table it's from?

If so, then, no, in PG, you need to know the sequence name to use currval()
(or nextval(), below).

> 2) Can I lock the sequence to make sure that another INSERT
>    doesn't occur before I select the currval() of the sequence?

As long as you're the only user on this connection, you don't have to lock
anything to achieve this -- currval() will give you the last serial #
specific for this backend, even if there have been other inserts.

-- J.

Joel BURTON | joel@joelburton.com | joelburton.com | aim: wjoelburton
Knowledge Management & Technology Consultant


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

Предыдущее
От: "Robert J. Sanford, Jr."
Дата:
Сообщение: Re: how to get id of last insert on a serial type?
Следующее
От: TPCCUVA
Дата:
Сообщение: space on disk of a table.