Skip site navigation (1) Skip section navigation (2)

Re: How to Return Table From Function

From: Rob Sargentg <robjsargent(at)gmail(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: How to Return Table From Function
Date: 2012-01-22 21:42:51
Message-ID: 4F1C82DB.8090207@gmail.com (view raw or flat)
Thread:
Lists: pgsql-sql
On 01/22/2012 06:09 AM, Rehan Saleem wrote:
> hi , i have created this function
>
> CREATE OR REPLACE FUNCTION totaloverlapcount(user_id integer , bg 
> integer ,center_distance integer)
> RETURNS varchar AS $$
>
> DECLARE percentage record;
> BEGIN
>
>
>
> select fname, lname, count(userid) totalcount 
> ,100.00*count(useriddetails)/totaluser into percentage
> from users
> where userid= user_id and bloodgroup>=bg and
> (centredistance<=center_distance or center_distance=1)
> group by fname, lname, user_id;
> return percentage;
>
>
> its just a dummy function , but all i want to know that how can i 
> return fname , lname totalcount and percentage from this function in 
> the form of table , not the return type varchar.
> thanks

-Sorry cannot get to a server at the moment to check this sql, but if 
you want to address the field of the result table you need to create the 
type for the returned table.

create type reportTypeName(fname text, lname text, totalcount integer);
CREATE OR REPLACE FUNCTION totaloverlapcount(user_id integer , bg 
integer ,center_distance integer)
RETURNS setof reportTypeName AS $$


Then you can do
  select lname, totalcount as tally from totaloverlapcount( id, bg, cdist);


In response to

pgsql-sql by date

Next:From: Francisco CalderónDate: 2012-01-25 14:12:05
Subject: Problem with sequence increment
Previous:From: Pavel StehuleDate: 2012-01-22 13:53:46
Subject: Re: How to Return Table From Function

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group