Re: Quoting Problem in PLPGSQL

From: Oliver Elphick <olly(at)lfix(dot)co(dot)uk>
To: Alex <alex(at)meerkatsoft(dot)com>
Cc: PostgreSQL general list <pgsql-general(at)postgresql(dot)org>
Subject: Re: Quoting Problem in PLPGSQL
Date: 2003-11-18 09:55:19
Message-ID: 1069149318.16543.742.camel@linda.lfix.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, 2003-11-18 at 08:54, Alex wrote:
> Hi,
>
> i have a problem with quoting in plpgsql. I try to insert dynamically
> some conditions into a select statment but for some reason it does not work.
>
> condition := '' AND price > 500 '';
>
> SELECT INTO tmpRec * WHERE xxxxxx || condition || ORDER BY userId ;
> What is the correct way to do this ?

FOR tmpRec IN EXECUTE ''SELECT INTO tmpRec * WHERE xxxxxx '' ||
condition || '' ORDER BY userId'' LOOP
...statements...
END LOOP;

>From the manual:
37.6.4. Executing Dynamic Commands
Oftentimes you will want to generate dynamic commands inside
your PL/pgSQL functions, that is, commands that will involve
different tables or different data types each time they are
executed. PL/pgSQL's normal attempts to cache plans for commands
will not work in such scenarios. To handle this sort of problem,
the EXECUTE statement is provided:

EXECUTE command-string;

where command-string is an expression yielding a string (of type
text) containing the command to be executed. This string is fed
literally to the SQL engine.

Note in particular that no substitution of PL/pgSQL variables is
done on the command string. The values of variables must be
inserted in the command string as it is constructed.

--
Oliver Elphick Oliver(dot)Elphick(at)lfix(dot)co(dot)uk
Isle of Wight, UK http://www.lfix.co.uk/oliver
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839 932A 614D 4C34 3E1D 0C1C
========================================
"A Song for the sabbath day. It is a good thing to
give thanks unto the LORD, and to sing praises unto
thy name, O most High." Psalms 92:1

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Andrey Mosienko 2003-11-18 10:50:59 Array syntax
Previous Message Peter Eisentraut 2003-11-18 09:40:38 Re: how to get historical data?