Problem with the result set of postgres

Lists: pgsql-sql
From: "Sandeep Chibber" <sandeep(at)vreach(dot)net>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: Problem with the result set of postgres
Date: 2002-05-27 07:22:47
Message-ID: 000f01c2054f$4d79ac10$1302a8c2@multicast.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

Hi
I am listing out the problem I am facing while EXECUTING the function .

create table newtest (name varchar(50), address varchar(50))

CREATE FUNCTION newtestfunc () RETURNS setof varchar AS
'SELECT name,address FROM newtest'
LANGUAGE 'SQL';

select newtestfunc()

The output is 168269272 ,
in this table there are two records and the output is also coming twice There is no change in the number 168269272 ,168269272

Please guide me how to get the result set through a java bean

My Requirement :

1.I want to retrieve multiple records from the table using function. The function call will come from the java bean.
2. The table will have different datatypes.

Thanks


From: Joe Conway <mail(at)joeconway(dot)com>
To: Sandeep Chibber <sandeep(at)vreach(dot)net>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Problem with the result set of postgres
Date: 2002-05-27 23:18:06
Message-ID: 3CF2BEAE.7020207@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

Sandeep Chibber wrote:
> in this table there are two records and the output is also coming
> twice There is no change in the number 168269272 ,168269272
>
> Please guide me how to get the result set through a java bean
>
> *My Requirement :*
>
> 1.I want to retrieve multiple records from the table using function.
> The function call will come from the java bean.
>
> 2. The table will have different datatypes.
>

Support for returning setof composite is limited in 7.2.x and before.
This works on 7.2:

test=# select version();
version
-----------------------------------------------------------
PostgreSQL 7.2 on i686-pc-linux-gnu, compiled by GCC 2.96
(1 row)

test=# create table newtest (name varchar(50), address varchar(50));

insert into newtest(name,address) values('a','b');
insert into newtest(name,address) values('c','d');

CREATE FUNCTION newtestfunc () RETURNS setof newtest AS
'SELECT name,address FROM newtest'
LANGUAGE 'SQL';

select name(newtestfunc()), address(newtestfunc());CREATE
test=#
test=# insert into newtest(name,address) values('a','b');
INSERT 41947 1
test=# insert into newtest(name,address) values('c','d');
INSERT 41948 1
test=#
test=# CREATE FUNCTION newtestfunc () RETURNS setof newtest AS
test-# 'SELECT name,address FROM newtest'
test-# LANGUAGE 'SQL';
CREATE
test=#
test=# select name(newtestfunc()), address(newtestfunc());
name | address
------+---------
a | b
c | d
(2 rows)

Note that the function returns "setof newtest", not "setof varchar"
because varchar is a base, scalar type. You want to return a composite
type, which means you need a table defined which reflects the return
tuple number and type of attributes. The numbers that you were getting
are pointers to the result tuples, but you can get at the individual
fields as shown above.

HTH,

Joe