Skip site navigation (1) Skip section navigation (2)

Re: MySQL LAST_INSERT_ID() to Postgres

From: Magnus Hagander <magnus(at)hagander(dot)net>
To: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
Cc: Russ Brown <pickscrape(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: MySQL LAST_INSERT_ID() to Postgres
Date: 2008-08-29 07:41:14
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-general
Alvaro Herrera wrote:
> Russ Brown escribió:
>> Masis, Alexander (US SSA) wrote:
>>> pg_get_serial_sequence('my_tbl_name','id_col_name'));"
>> Any reason why you can't just do this?
>> CREATE FUNCTION last_insert_id() RETURNS bigint AS $$
>>   SELECT lastval();
> If your table has a trigger that inserts into another table with its own
> sequence, you're screwed.

I assume you're equally screwed with MySQL LAST_INSERT_ID() in that case
- so it'd be bug compatible.

I know MSSQL had similar issues with the use of @@IDENTITY - which is
why they went down the ugly path of SCOPE_IDENTITY(). Why they didn't
just implement RETURNING, I have no idea...


In response to


pgsql-general by date

Next:From: J WelcomecertDate: 2008-08-29 12:39:44
Subject: warning:, needed by
Previous:From: Laura Del CañoDate: 2008-08-29 07:02:31
Subject: Re: pg_dump problem

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group