Обсуждение: parse error at or near $1

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

parse error at or near $1

От
Michael McDonnell
Дата:
I have created the following pl/pgsql function:

     CREATE FUNCTION contactable_name (integer) RETURNS text AS '
       DECLARE
         myid ALIAS FOR $1;
         contact_table text;
       BEGIN
         SELECT INTO contact_table t.table_name FROM contactable c,
     contactable_type t WHERE c.id = $1 AND c.type_id = t.id;
         RETURN EXECUTE contact_table || ''_name('' || myid ||
     '')'';
       END;
     ' LANGUAGE 'plpgsql';

And when I run "SELECT contactable_name(1);" I get the error message
"parse error at or near $1".

The purpose of this function is to return the output of another
function.  It dynamically builds the function name based on the name of
the table corresponding to the type of thing it is building the name
for.

--
MM


Re: parse error at or near $1

От
Tom Lane
Дата:
Michael McDonnell <michael@winterstorm.ca> writes:
> And when I run "SELECT contactable_name(1);" I get the error message
> "parse error at or near $1".

$1 is a parameter placeholder; probably your problem is unexpected
substitution (or lack of substitution) of a plplgsql variable or
parameter into a query that's being sent to the underlying SQL engine.

In 7.1 the easiest way to debug this type of problem is to do
    SET debug_print_query TO 1;
then execute the problem function call, and then look in the postmaster
log to see what queries got generated by the function.  In older PG
versions there is no debug_print_query variable, so you have to resort
to setting environment variable PGOPTIONS to "-d2" before starting psql
to get query logging.

plpgsql needs more support for debugging --- some kind of trace
mechanism that doesn't require access to the postmaster log would be
awfully nice ...

            regards, tom lane