pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
On Insert Do Instead Select....
2+ messages / 2 participants
[nested] [flat]

* On Insert Do Instead Select....
@ 2011-12-30 00:30  Daniel Jeffery <daniel.viviotech@gmail.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Daniel Jeffery @ 2011-12-30 00:30 UTC (permalink / raw)
  To: pgsql-jdbc

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 FROM
test WHERE column_name = NEW.column_name;*
*
*
*
*
How would I get the JDBC driver to return the resultset?

-- Daniel

^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: On Insert Do Instead Select....
@ 2011-12-30 11:06  Thomas Kellerer <spam_eater@gmx.net>
  parent: Daniel Jeffery <daniel.viviotech@gmail.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Thomas Kellerer @ 2011-12-30 11:06 UTC (permalink / raw)
  To: 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.






^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2011-12-30 11:06 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2011-12-30 00:30 On Insert Do Instead Select.... Daniel Jeffery <daniel.viviotech@gmail.com>
2011-12-30 11:06 ` Thomas Kellerer <spam_eater@gmx.net>

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