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

Re: INSERT and get ID

From: Dave Cramer <pg(at)fastcrypt(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:17:56
Message-ID: F2314408-011B-41C3-A57F-33815A9669A9@fastcrypt.com (view raw or flat)
Thread:
Lists: pgsql-jdbc
On 27-Jul-05, at 8:56 AM, Peter(dot)Zoche(at)materna(dot)de wrote:

> Hi!
>
> Say I have done the following:
>
> CREATE SEQUENCE myseq
>
> CREATE TABLE foo (
> id INTEGER,
> val INTEGER,
> PRIMARY KEY( id ) );
>
> Is there an easy way to get the id of the newly inserted data set?
> So if I do the following:
>
> 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? 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. (using postgresql 8.0 JDBC3) I googled but I did  
> only find
> PostgreSQL-
> specific hints, no one for JDBC.
you can use select currval('myseq') after using nextval('myseq') this  
will retrieve the value recently returned by nextval
in your session ( so it will not be over written by another connection )


>
> Thanks, Peter
>
> ---------------------------(end of  
> broadcast)---------------------------
> TIP 1: if posting/reading through Usenet, please send an appropriate
>        subscribe-nomail command to majordomo(at)postgresql(dot)org so that  
> your
>        message can get through to the mailing list cleanly
>
>


In response to

pgsql-jdbc by date

Next:From: Roland WalterDate: 2005-07-27 13:25:58
Subject: Re: INSERT and get ID
Previous:From: Oliver JowettDate: 2005-07-27 13:14:56
Subject: Re: INSERT and get ID

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