Re: Retrieving last InsertedID : INSERT... RETURNING safe ? - Mailing list pgsql-jdbc

From Heikki Linnakangas
Subject Re: Retrieving last InsertedID : INSERT... RETURNING safe ?
Date
Msg-id 47BC0AF6.2060500@enterprisedb.com
Whole thread Raw
In response to Retrieving last InsertedID : INSERT... RETURNING safe ?  (<spastor@motionsponsor.com>)
Responses Re: Retrieving last InsertedID : INSERT... RETURNING safe ?  (Guillaume Cottenceau <gc@mnc.ch>)
List pgsql-jdbc
spastor@motionsponsor.com wrote:
> Only one exception is regarding the best method to retrieve the last inserted id (serial). There are many posts on
thistopic but i couldn t find a definitive response.  
> I am using postgresql version 8.2.6 and jdbc  8.2_p505, i ve been experiencing INSERT statement with RETURNING myID,
usingan executeQuery it works great, i retrieve the proper value everytime at least  from the few tests i have been
doin locally. 
> Is it THAT easy or could this method break somehow in a live environement ?

It is that easy, and that is a good way to do it.

Alternatively, if you need to support older PostgreSQL version for
example, you could use a "SELECT currval('sequence')" query after the
INSERT.

Much of the of discussion you saw were probably about implementing the
JDBC getGeneratedKeys() function that some applications need for
portability reasons, or because of middleware that doesn't know how to
do INSERT RETURNING. PostgreSQL driver doesn't support that at the
moment, but as long as you don't need it, you'll be fine.

--
   Heikki Linnakangas
   EnterpriseDB   http://www.enterprisedb.com

pgsql-jdbc by date:

Previous
From:
Date:
Subject: Retrieving last InsertedID : INSERT... RETURNING safe ?
Next
From:
Date:
Subject: Re: Retrieving last InsertedID : INSERT... RETURNING safe ?