Обсуждение: Quoting Problem in PLPGSQL

Поиск
Список
Период
Сортировка

Quoting Problem in PLPGSQL

От
Alex
Дата:
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 ?

Thanks
Alex



Re: Quoting Problem in PLPGSQL

От
Oliver Elphick
Дата:
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.Elphick@lfix.co.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