Re: Error >>syntax error<< at >>$1<< at character 53 - Mailing list pgsql-general

From Tom Lane
Subject Re: Error >>syntax error<< at >>$1<< at character 53
Date
Msg-id 23679.1092011225@sss.pgh.pa.us
Whole thread Raw
In response to Error >>syntax error<< at >>$1<< at character 53  (Thiemo Kellner <thiemo@thiam.ch>)
List pgsql-general
Thiemo Kellner <thiemo@thiam.ch> writes:
> I try to code a PL/pgSQL function, but always get to an execution error, I =
> am=20
> unable to solve:

> create or replace function GET_SUMMARY() returns integer as '
>    declare
>       STR_SOURCE_NAME         constant pg_catalog.pg_views.viewname%type
>                                     :=3D \'BUCHUNGEN\';
>       STR_OBJEKTVIEW          constant pg_catalog.pg_views.viewname%type
        ^^^^^^^^^^^^^^
>                                     :=3D \'OBJEKTHIERARCHIE\';
>       R_OBJEKTHIERARCHIE      record;
>       TXT                     text;
>    begin
>       for R_OBJEKTHIERARCHIE in
>          select
>                OBJEKTKATEGORIE,
>                OBJEKTGRUPPE,
>                OBJEKT
>             from
>                STR_OBJEKTVIEW
                 ^^^^^^^^^^^^^^
>       loop

Don't use plpgsql variables named the same as tables or fields that you
need to reference in queries inside the function.  plpgsql will think it
ought to substitute the variable value as a parameter.

8.0devel generates a more useful error message from this function:

regression=# select get_summary();
ERROR:  syntax error at or near "$1" at character 53
QUERY:   select OBJEKTKATEGORIE, OBJEKTGRUPPE, OBJEKT from  $1
CONTEXT:  PL/pgSQL function "get_summary" line 9 at for over select rows
LINE 1:  select OBJEKTKATEGORIE, OBJEKTGRUPPE, OBJEKT from  $1
                                                            ^

Here you can actually see the query string fed to the SQL engine,
and see that STR_OBJEKTVIEW got replaced by a parameter symbol,
which of course is not going to work.

            regards, tom lane

pgsql-general by date:

Previous
From: Clodoaldo Pinto Neto
Date:
Subject: How copy a new line char to a file?
Next
From: Kris Jurka
Date:
Subject: Re: pg jdbc driver