pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Thomas Kellerer <spam_eater@gmx.net>
To: pgsql-jdbc@postgresql.org
Subject: Re: On Insert Do Instead Select....
Date: Fri, 30 Dec 2011 12:06:07 +0100
Message-ID: <jdk5ur$kho$2@dough.gmane.org> (raw)
In-Reply-To: <CAJjQhZCQYp1nBYokc1ySOtXZbDu==gGG=_qPtonh42rhMxeipg@mail.gmail.com>
References: <CAJjQhZCQYp1nBYokc1ySOtXZbDu==gGG=_qPtonh42rhMxeipg@mail.gmail.com>

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.






view thread (2+ messages)

Message-ID: <jdk5ur$kho$2@dough.gmane.org>
Permalink:  ../jdk5ur$kho$2@dough.gmane.org/
Also on:    postgresql.org/message-id/jdk5ur$kho$2@dough.gmane.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: spam_eater@gmx.net
  Subject: Re: On Insert Do Instead Select....
  In-Reply-To: <jdk5ur$kho$2@dough.gmane.org>
  References: <CAJjQhZCQYp1nBYokc1ySOtXZbDu==gGG=_qPtonh42rhMxeipg@mail.gmail.com> <jdk5ur$kho$2@dough.gmane.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox