How to quote in plpgsql function for Execute dynamic queries

Поиск
Список
Период
Сортировка
От Phillip J. Allen
Тема How to quote in plpgsql function for Execute dynamic queries
Дата
Msg-id 3B7E5B2E.1054CF9E@attglobal.net
обсуждение исходный текст
Ответ на using the Text::Query perl module with postgresql  (harrold@sage.che.pitt.edu)
Ответы Re: How to quote in plpgsql function for Execute dynamic queries  (Tom Lane <tgl@sss.pgh.pa.us>)
Список pgsql-novice
Hi all,

I am trying to write a simple plpgsql function that executes a dynamic
function and cannot get the quotes right even after reading the programmer
manual.  I just can't seem to get my head around it.  This is what I am trying
to do.

CREATE FUNCTION myfunc(float8) RETURNS float8 AS '
    DECLARE
        dpsql varchar;
        dprec RECORD;
        a float8;
        f float8;
    BEGIN
        dpsql := 'Select d.a_parm, d.f_parm, d.deltax, d.deltay FROM c_g_datum
WHERE d.datum_id = ' || $1 || ';';      --this sql will only return 1 record

        FOR dprec IN EXCECUTE dpsql LOOP
            a := dprec.a_parm;
            f := dprec.f_parm;
        END LOOP;
--    DO SOME CALCUATIONS AND RETURN A FLOAT8 VALUE;
    END;'
LANGUAGE 'plpgsql';

So the real question is how do I formate the dpsql string.  I have returned
the string and executed an identical string in a querry and it works but for
some reason it bails out in an error when executed dynamically.

Does anyone have any fuctions that demonstrate how to properly quote
concatenated strings?  Even after reading the manual I am confused.  Thanks

Phillip J. Allen
Consulting Geochemist/Geologist
Lima Peru
e-mail: paallen@attglobal.net



В списке pgsql-novice по дате отправления:

Предыдущее
От: Tom Robinson
Дата:
Сообщение: How to find size of a table
Следующее
От: Tom Lane
Дата:
Сообщение: Re: How to quote in plpgsql function for Execute dynamic queries