Re: INSERT and get ID

From: Oliver Jowett <oliver(at)opencloud(dot)com>
To: Peter(dot)Zoche(at)materna(dot)de
Cc: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: INSERT and get ID
Date: 2005-07-27 13:14:56
Message-ID: 42E788D0.5050902@opencloud.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Peter(dot)Zoche(at)materna(dot)de wrote:

> String insert = "INSERT INTO foo (id, val)
> VALUES( nextval('myseq'), 5 )";
> int count = statement.executeUpdate( insert );
>
> I only get the row count of inserted rows. Do I have to query the
> database again to get the id?

Currently, yes. You can usually do some trickery with currval() to run
both an INSERT and a SELECT in the same statement and avoid an extra
round-trip.

> Or is there another way? I have tried
> statement.executeUpdate( insert, Statement.RETURN_GENERATED_KEYS);
> but I get an PSQLException saying that the feature of auto-generated keys
> is not supported.

Right, this really needs server-side support before we can implement it
(INSERT ... RETURNING ... or similar)

-O

In response to

Browse pgsql-jdbc by date

  From Date Subject
Next Message Dave Cramer 2005-07-27 13:17:56 Re: INSERT and get ID
Previous Message Peter.Zoche 2005-07-27 12:56:59 INSERT and get ID