Re: PL/pgSQL function syntax question? - Mailing list pgsql-sql

From aklaver@comcast.net (Adrian Klaver)
Subject Re: PL/pgSQL function syntax question?
Date
Msg-id 090820082147.15642.48C59D7A00071B2600003D1A22007374789D0A900E04050E@comcast.net
Whole thread Raw
In response to PL/pgSQL function syntax question?  ("Ruben Gouveia" <rubes7202@gmail.com>)
Responses Re: PL/pgSQL function syntax question?  (imad <immaad@gmail.com>)
List pgsql-sql
-------------- Original message ----------------------
From: "Ruben Gouveia" <rubes7202@gmail.com>
> i get the following error when i try and create the following function:
>
> Basically, i am trying to have two different dates compared and only the
> most recent returned to me.  This seems pretty straight forward, what I am
> doing wrong here?
>
> create or replace function fcn_pick_date(v_dt date)
> returns date as $$
>   DECLARE
>       v_dt date;
>   BEGIN
>     for v_record in select last_periodic, last_boot
>         from mediaportal
>     loop
>     if v_dt >= v_record.last_boot then
>        v_dt := v_record.last_periodic;
>     else
>         v_dt := v_record.last_boot;
>     end if;
>     end loop;
>     return (v_dt);
> END;
> $$ LANGUAGE 'plpgsql';
>
>
> ERROR:  loop variable of loop over rows must be record or row variable at or
> near "loop" at character 195
>
> ********** Error **********
>
> ERROR: loop variable of loop over rows must be record or row variable at or
> near "loop"
> SQL state: 42601
> Character: 195

You need to DECLARE v_record as a RECORD variable.
v_record RECORD;

--
Adrian Klaver
aklaver@comcast.net




pgsql-sql by date:

Previous
From: "Ruben Gouveia"
Date:
Subject: PL/pgSQL function syntax question?
Next
From: imad
Date:
Subject: Re: PL/pgSQL function syntax question?