Re: MySQL LAST_INSERT_ID() to Postgres

Поиск
Список
Период
Сортировка
От Scott Marlowe
Тема Re: MySQL LAST_INSERT_ID() to Postgres
Дата
Msg-id dcc563d10808281209o5099a45asb3accdea36190e01@mail.gmail.com
обсуждение исходный текст
Ответ на MySQL LAST_INSERT_ID() to Postgres  ("Masis, Alexander \(US SSA\)" <alexander.masis@baesystems.com>)
Ответы Re: MySQL LAST_INSERT_ID() to Postgres  (Steve Atkins <steve@blighty.com>)
Список pgsql-general
On Thu, Aug 28, 2008 at 10:14 AM, Masis, Alexander (US SSA)
<alexander.masis@baesystems.com> wrote:
> I was mapping C++ application code that works with mySQL to work with
> Postgres.
> There were a number of articles on line regarding the conversion from
> mySQL to Postgres like:
SNIP
> Well, in MySQL it's easy you just do:
> "SELECT LAST_INSERT_ID();"
>        In Postgres, however it is not that simple. You have to know the
> name of so called "insert sequence". Postgres has a system function for
> that( SQL line below ).
> In Postgres you will have to provide the table and column name(
> "auto_increment" type in MySQL or "serial or bigserial" in Postgres).
>
> Here is that SQL query that returns the last inserted ID:
>
>   "SELECT CURRVAL(
> pg_get_serial_sequence('my_tbl_name','id_col_name'));"

That's the hard way.  Starting with pgsql 8.2 you can do it much more easily:

create table tester (id serial primary key, info text);
insert into tester (info) values ('this is a text string') returning id;

tada!  All done, that insert will return the id for you.

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

Предыдущее
От: "Scott Marlowe"
Дата:
Сообщение: Re: importing dates
Следующее
От: "Dave Page"
Дата:
Сообщение: Re: temp schemas