Re: INSERT and get ID

Поиск
Список
Период
Сортировка
От Oliver Jowett
Тема Re: INSERT and get ID
Дата
Msg-id 42E788D0.5050902@opencloud.com
обсуждение исходный текст
Ответ на INSERT and get ID  (Peter.Zoche@materna.de)
Список pgsql-jdbc
Peter.Zoche@materna.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

В списке pgsql-jdbc по дате отправления:

Предыдущее
От: Peter.Zoche@materna.de
Дата:
Сообщение: INSERT and get ID
Следующее
От: Dave Cramer
Дата:
Сообщение: Re: INSERT and get ID