Re: stored procedure returning result set.

From: Kris Jurka <books(at)ejurka(dot)com>
To: "Gohil, Hemant" <HGohil(at)dylt(dot)com>
Cc: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: stored procedure returning result set.
Date: 2008-09-23 20:15:38
Message-ID: Pine.BSO.4.64.0809231610230.22227@leary.csoft.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

On Tue, 23 Sep 2008, Gohil, Hemant wrote:

> I am trying to convert a working SQL query to stored function and get
> the resultset back.
>
> select * FROM sp_allCodes(1542) AS
> myresult(firstColumn,codeid,category,categoryid,CdLnk,codevalue,allcodes
> desc,codelink,maskfmt,sortseqnumber,adddate,changedate,addopid,changeopi
> d,allcodeslongdesc)
>
> ERROR: a column definition list is required for functions returning
> "record"

You need type information as well in the output list for record returning
functions.

> Basically I am using Sybase ASE presently and I am exploring the option
> to migrate to PostgreSQL, for Sybase I would just write
>
> Execute sp_allCodes 1542
>
> and it will return the result set with all the columns and rows. Is
> there a way to achieve similar functionality ?
>

To use "setof record" you must explicitly name the output colums in the
select. Other options are to create a new type to represent the output of
the function ("returning setof mytype") or to use output parameters. In
that case you can just say "select * from myfunc()".

http://jdbc.postgresql.org/documentation/83/callproc.html#callproc-resultset-setof

The final option is to return a refcursor which you can then turn into a
ResultSet. This is the most flexible as it doesn't require naming the
columns during function creation or function execution.

http://jdbc.postgresql.org/documentation/83/callproc.html#callproc-resultset-refcursor

Kris Jurka

In response to

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Michael Enke 2008-09-23 20:32:13 Re: PreparedStatement.toString() creates valid SQL; was: Q: use setObject also for int or string
Previous Message Gohil, Hemant 2008-09-23 19:09:54 stored procedure returning result set.