Thread: plpgsql question

plpgsql question

From
"Gauthier, Dave"
Date:

v8.3.4 on linux

 

Is there a way to set the query used in a "for rec in (query) loop -> end loop" be a variable?  Example

 

if (foo = 'whatever')

then

  sqlstmt := "select x,y,z ...";

else

  sqlstmt := "select a,b,c ...";

end if ;

 

for therec in

  sqlstmt

loop

    ...

end loop;

 

 

Thanks in Advance for any help. 

Re: plpgsql question

From
Adrian Klaver
Date:
On Monday, December 05, 2011 6:59:32 am Gauthier, Dave wrote:
> v8.3.4 on linux
>
> Is there a way to set the query used in a "for rec in (query) loop -> end
> loop" be a variable?  Example
>
> if (foo = 'whatever')
> then
>   sqlstmt := "select x,y,z ...";
> else
>   sqlstmt := "select a,b,c ...";
> end if ;
>
> for therec in
>   sqlstmt
> loop
>     ...
> end loop;

http://www.postgresql.org/docs/8.3/interactive/plpgsql-control-structures.html#PLPGSQL-RECORDS-ITERATING

If I am following the above right then  the: FOR target IN EXECUTE text_expression LOOP form is what you are
looking for.  I have not used variable substitution for this form, so you will need to test.

>
>
> Thanks in Advance for any help.

--
Adrian Klaver
adrian.klaver@gmail.com