Re: MySQL LAST_INSERT_ID() to Postgres - Mailing list pgsql-general

From Alvaro Herrera
Subject Re: MySQL LAST_INSERT_ID() to Postgres
Date
Msg-id 20080828222246.GH8424@alvh.no-ip.org
Whole thread Raw
In response to Re: MySQL LAST_INSERT_ID() to Postgres  (Russ Brown <pickscrape@gmail.com>)
Responses Re: MySQL LAST_INSERT_ID() to Postgres  (Magnus Hagander <magnus@hagander.net>)
List pgsql-general
Russ Brown escribió:
> Masis, Alexander (US SSA) wrote:
> >
> >    "SELECT CURRVAL(
> > 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();
> $$ LANGUAGE SQL VOLATILE;

If your table has a trigger that inserts into another table with its own
sequence, you're screwed.

--
Alvaro Herrera                                http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.

pgsql-general by date:

Previous
From: Joshua Drake
Date:
Subject: Re: MySQL LAST_INSERT_ID() to Postgres
Next
From: "D. Dante Lorenso"
Date:
Subject: Re: MySQL LAST_INSERT_ID() to Postgres