Re: problems returning a resultset from a function

From: Oliver Jowett <oliver(at)opencloud(dot)com>
To: Leo Martin Orfei <orfeileo(at)yahoo(dot)com>
Cc: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: problems returning a resultset from a function
Date: 2004-09-11 07:40:44
Message-ID: 4142ABFC.6020401@opencloud.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-jdbc

Leo Martin Orfei wrote:

[.. declare function returning refcursor ..]

> CallableStatement cs = null;
> ResultSet rs = null;
> cs = con.prepareCall("{ ? = call test()}");
> cs.registerOutParameter(1, java.sql.Types.OTHER);
> rs = cs.executeQuery();
> rs.next();
> System.out.println("name: " +rs.getString(1));
>
> but throws te following error:
>
> cursor "<unnamed portal 1>" does not exist

Check that you have called Connection.setAutoCommit(false). If
autocommit is on, your returned cursor will be closed as soon as the
transaction (auto)commits, so the subsequent select done to fetch the
portal contents will fail.

A '? = call' escape does not return a resultset. You should use the
CallableStatement.get...() methods to retrieve the values of the out
parameter. You will likely see errors complaining about no resultset
being returned from executeQuery() once you fix the autocommit setting.

The refcursor is returned as a ResultSet (as the out-parameter value)
i.e. CallableStatement.getObject(1) will return a ResultSet that has the
contents of the refcursor.

See
http://www.postgresql.org/docs/current/static/jdbc-callproc.html for
some example code on using callable statements and refcursor-returning
functions. (note that using PGRefCursorResultSet is deprecated; just use
getString() to obtain the cursor name).

-O

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Anton Nikiforov 2004-09-11 07:42:46 pg_shadow in a constraint
Previous Message Thomas F.O'Connell 2004-09-11 07:35:31 Re: Auto increment/sequence on multiple columns?

Browse pgsql-jdbc by date

  From Date Subject
Next Message Leo Martin Orfei 2004-09-11 15:23:48 Re: problems returning a resultset from a function
Previous Message Oliver Jowett 2004-09-11 07:21:33 Re: Postgres 8 & JBuilder X