Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 5B817633D1A for ; Mon, 13 Jul 2009 18:55:36 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 89535-01-3 for ; Mon, 13 Jul 2009 18:55:25 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from ciao.gmane.org (main.gmane.org [80.91.229.2]) by mail.postgresql.org (Postfix) with ESMTP id DDEE6635D75 for ; Mon, 13 Jul 2009 18:55:13 -0300 (ADT) Received: from list by ciao.gmane.org with local (Exim 4.43) id 1MQTUM-0001EI-Fd for pgsql-jdbc@postgresql.org; Mon, 13 Jul 2009 21:55:10 +0000 Received: from ppp-93-104-90-24.dynamic.mnet-online.de ([93.104.90.24]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 13 Jul 2009 21:55:10 +0000 Received: from spam_eater by ppp-93-104-90-24.dynamic.mnet-online.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 13 Jul 2009 21:55:10 +0000 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-jdbc@postgresql.org From: Thomas Kellerer Subject: Re: DELETE ... RETURNING Date: Mon, 13 Jul 2009 23:54:55 +0200 Lines: 47 Message-ID: References: <226a19190907131251m6c3786f5id8e8025f5c53cf66@mail.gmail.com> <4A5BAA29.9000004@opencloud.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: ppp-93-104-90-24.dynamic.mnet-online.de 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: <4A5BAA29.9000004@opencloud.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200907/28 X-Sequence-Number: 19796 Oliver Jowett wrote on 13.07.2009 23:42: > Thomas Kellerer wrote: > >> At least executeUpdate() should not throw an exception. > > Wrong, see the javadoc: Oops ;) > Your SQL statement is returning a ResultSet object, so executeUpdate > correctly throws an exception. > > Use executeQuery() or execute(). But execute() will not give the information about the returned IDs as getMoreResults() always returns false. And in my test getUpdateCount() returned -1 even though rows were deleted. The following code will print "deleted: -1" and nothing more. But my understanding is, that it should print "deleted: 3", and then iterate over the returned ids (but at least show the correct update count) Statement stmt = con.createStatement(); stmt.executeUpdate( "CREATE TABLE test_delete (id integer primary key, some_data varchar(100))" ); stmt.executeUpdate("insert into test_delete values (1, 'first row')"); stmt.executeUpdate("insert into test_delete values (2, 'second row')"); stmt.executeUpdate("insert into test_delete values (3, 'third row')"); con.commit(); stmt.execute("delete from test_delete returning id"); System.out.println("deleted: " + stmt.getUpdateCount()); if (stmt.getMoreResults()) { System.out.println("has result"); rs = stmt.getResultSet(); while (rs.next()) { System.out.println(rs.getObject(1)); } } Thomas