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

Re: MySQL LAST_INSERT_ID() to Postgres

From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Russ Brown <pickscrape(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: MySQL LAST_INSERT_ID() to Postgres
Date: 2008-08-28 22:22:46
Message-ID: 20080828222246.GH8424@alvh.no-ip.org (view raw or flat)
Thread:
Lists: 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.

In response to

Responses

pgsql-general by date

Next:From: D. Dante LorensoDate: 2008-08-28 22:23:36
Subject: Re: MySQL LAST_INSERT_ID() to Postgres
Previous:From: Matthew DennisDate: 2008-08-28 22:21:59
Subject: Re: indexes on functions and create or replace function

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