Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 966AEC24693 for ; Fri, 30 Dec 2011 07:10:22 -0400 (AST) Received: from lo.gmane.org ([80.91.229.12]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RgaLq-0007Ko-Vq for pgsql-jdbc@postgresql.org; Fri, 30 Dec 2011 11:10:23 +0000 Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1RgaLd-0007ZQ-V6 for pgsql-jdbc@postgresql.org; Fri, 30 Dec 2011 12:10:05 +0100 Received: from host-188-174-161-46.customer.m-online.net ([188.174.161.46]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 30 Dec 2011 12:10:05 +0100 Received: from spam_eater by host-188-174-161-46.customer.m-online.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 30 Dec 2011 12:10:05 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-jdbc@postgresql.org From: Thomas Kellerer Subject: Re: On Insert Do Instead Select.... Date: Fri, 30 Dec 2011 12:06:07 +0100 Lines: 25 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: host-188-174-161-46.customer.m-online.net User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.21) Gecko/20090302 Thunderbird/2.0.0.21 Mnenhy/0.7.5.666 In-Reply-To: X-Pg-Spam-Score: -5.5 (-----) X-Archive-Number: 201112/86 X-Sequence-Number: 22540 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 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.