Re: question on setof record returning plpgsql function - Mailing list pgsql-general

From Julie May
Subject Re: question on setof record returning plpgsql function
Date
Msg-id 036501c38ec0$2f6ccb70$a1d2a8c0@impsu.net
Whole thread Raw
In response to question on setof record returning plpgsql function  ("Julie May" <julie@ccorb.com>)
List pgsql-general
 Joe,

 Thank you very much. I didn't even think of casting the result of the first
 loop. I will test out the function tomorrow with the cast included and let
 you know how it works. From the looks of it (your results) it should work
 fine.

 Joe Conway Wrote:
I think you had it working when you got the unexpected return type
message, you were just calling it wrong. The data types must match
*exactly* including WITH/WITHOUT TIME ZONE. I often find it simpler when
working with date/time data types to use an explicit cast, e.g.:

-- Function: public.get_factory_ytd()
CREATE or replace FUNCTION public.get_factory_ytd() RETURNS setof record
AS '
declare
        ytd_record record;
        d_date record;
begin
   for d_date in select distinct delivery_date from load_info_table
order by delivery_date asc loop
       for ytd_record in execute
       ''select sum(clean_weight)/sum(dirty_weight) as tare,
''''''||d_date.delivery_date|| ''''''::date from load_info_table where
delivery_date <= ''''''||d_date.delivery_date|| '''''''' loop
       return next ytd_record ;
     end loop;
   end loop;
  return;
end'  LANGUAGE 'plpgsql' VOLATILE;

test=# select * from get_factory_ytd() as (tare float8, delivery_date date);
        tare        | delivery_date
-------------------+---------------
  0.829268292682927 | 2003-01-01
  0.803571428571429 | 2003-01-02
(2 rows)

Note the ::date that I stuck in the function and how I specified
delivery_date as type "date" in the query.

HTH,

Joe


pgsql-general by date:

Previous
From: Joe Conway
Date:
Subject: Re: undefined reference to 'pg_detoast_datum'
Next
From: Bruno Wolff III
Date:
Subject: Re: Parent Id