Re: On Insert Do Instead Select....

From: Thomas Kellerer <spam_eater(at)gmx(dot)net>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: On Insert Do Instead Select....
Date: 2011-12-30 11:06:07
Message-ID: jdk5ur$kho$2@dough.gmane.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Daniel Jeffery wrote on 30.12.2011 01:30:
> Is it at all possible for an executed insert statement to return a
> select statement resultset?
>
> Here's an example table:
>
> /CREATE TABLE test (/ / id integer NOT NULL DEFAULT nextval(
> 'test_id_seq'::regclass ),/ / column_name text/ /);/ / / / / Here's
> the table's rule:
>
> /CREATE RULE replace_test AS ON INSERT TO test WHERE EXISTS( SELECT 1
> FROM test WHERE (column_name = NEW.column_name) ) DO INSTEAD SELECT
> test.id <http://test.id> FROM test WHERE column_name =
> NEW.column_name;/ / / / / How would I get the JDBC driver to return
> the resultset?

I haven't tried it, but using Statement.execute() (instead of executeUpdate()) should do it.

Then you need to call getMoreResults(), getResults(), getUpdateCount() to get everything.

Pay attention to the javadocs of Statement.getUpdateCount() and Statement.getMoreResults() describing the condition how to detect if there are no more results/update counts to retrieve.

It's essentially the same as retrieving the data returned by an INSERT ... RETURNING.

In response to

Browse pgsql-jdbc by date

  From Date Subject
Next Message Andrew Dunstan 2012-01-02 21:09:54 Re: JDBC builds broken
Previous Message Daniel Jeffery 2011-12-30 00:30:21 On Insert Do Instead Select....