Thread: Strange error with Pl/PgSQL

Strange error with Pl/PgSQL

From
Laurent HERVE
Date:
I wrote a little test function in pl/pgsql :

create function TEST (char(5),char(6),char(11))
returns boolean
as '
DECLARE
        bank alias for $1;
        guichet alias for $2;
        compte alias for $3;
        cmpt record;
BEGIN
raise notice ''Banque %'',bank;
                select into cmpt * from compte;
--              where code_banque=bank
--              and numero_guichet=guichet
--              and numero_compte=compte;
                return ''t'';
END;'
LANGUAGE 'plpgsql';

When using this function I get the following error :
z_money=> select test('99999','00001','1');
ERROR:  parser: parse error at or near "$1"

It doesn't happen with another table than ''compte''. It looks like
there is something wrong with table ''compte'', but I still can do
selects, inserts, etc... with psql.
If I suppress parameters, the error message does not happen.

So what's wrong ? How can I found more information ?

Thanks for advance.



Re: Strange error with Pl/PgSQL

From
Tom Lane
Date:
Laurent HERVE <laurent_herve@infonie.fr> writes:
> create function TEST (char(5),char(6),char(11))
> returns boolean
> as '
> DECLARE
>         bank alias for $1;
>         guichet alias for $2;
>         compte alias for $3;
          ^^^^^^
>         cmpt record;
> BEGIN
> raise notice ''Banque %'',bank;
>                 select into cmpt * from compte;
                                          ^^^^^^
>                 return ''t'';
> END;'
> LANGUAGE 'plpgsql';

> ERROR:  parser: parse error at or near "$1"

> It doesn't happen with another table than ''compte''.

You've used compte as the name of a parameter in the function,
so the SELECT gets transformed to
                 select into cmpt * from $3;
which won't work.  (Although I'd have expected the error message
to complain about $3 not $1 ... hmm ... maybe something else is
going on here?)

            regards, tom lane