On Tue, 6 Mar 2007, Jeroen T. Vermeulen wrote: > On Tue, March 6, 2007 14:58, Bart Samwel wrote: > > Jim Stewart wrote: > > >> 1) Is it possible to obtain the ID of a row inserted via a prepared > >> statement? > > > > Try using the syntax "returning", i.e.: > > > This syntax is supported by PostgreSQL 8.2 and up, it was added in one > > of the more recent SQL standards. The trick also works for UPDATE > > statements AFAIK, and you can even do DELETE...RETURNING to return ids > > of the records you deleted. > > Bart, you're right. Before I sent out my own reply I did a quick check > for this syntax on a local database, and it wasn't supported. But I > missed the fact that my local database was still running on 8.1. Yes, > RETURNING is definitely the way to go.
My PostgreSQL ignorance came into play here. I didn't realize that OID was not the same as "last inserted ID". I got the ID I wanted by using currval() on the sequence. I'm hoping this is reliable if called inside the same transaction as the insert. Googling around seems to indicate that currval() is the right way to get the ID. I'll be switching to the new 'returning' feature as soon as our sysadmin upgrades the server to 8.2. Thanks for the help! Jim _______________________________________________ Libpqxx-general mailing list [email protected] http://gborg.postgresql.org/mailman/listinfo/libpqxx-general
