Re: pl/pgSQL variable substitution - Mailing list pgsql-general

From Alban Hertroys
Subject Re: pl/pgSQL variable substitution
Date
Msg-id F7D78339-9862-4B4B-BA55-242CA5A0C069@solfertje.student.utwente.nl
Whole thread Raw
In response to pl/pgSQL variable substitution  (Jeremy Palmer <JPalmer@linz.govt.nz>)
List pgsql-general
On 17 Feb 2011, at 5:33, Jeremy Palmer wrote:

> Hi,
>
> I'm creating a pl/pgSQL function that returns a table that has a column name which is the same as a PostgreSQL
reserved. In the below example a have returning table with a column called 'desc': 
>
> CREATE OR REPLACE FUNCTION bad_func() RETURNS TABLE (bar INTEGER, "desc" VARCHAR(100)) AS $$
> BEGIN
>    RETURN QUERY
>        SELECT foo.bar, foo."desc"
>        FROM foo
>        ORDER BY foo."desc" DESC;
> END;
> $$ LANGUAGE plpgsql;
>
> When I have a query that uses DESC reserved word within the function the following variable substitution occurs:
>
> ERROR:  syntax error at or near "$1"
> LINE 1:  SELECT foo.bar, foo."desc"   FROM foo."desc" ORDER BY  foo."desc"   $1
>                                                                             ^
>
> In my case I really would like to keep the table names i.e. no '_' etc.



Your problem isn't with your table names, but with your parameter names. The "desc" parameter from your function
declarationmatches the DESC keyword in your query, from the looks of it. 
Either use the old unnamed function declaration and use $1 and $2 in your function, or rename your parameters to
somethingthat won't be in your queries as something else than a parameter reference. 

That said, I don't see where in your function you intend to use those parameters. Possibly you're trying to create a
dynamicquery? Pavel answered that part of your question already. 

Alban Hertroys

--
Screwing up is an excellent way to attach something to the ceiling.


!DSPAM:737,4d5ccf3211731594261662!



pgsql-general by date:

Previous
From: Pavel Stehule
Date:
Subject: Re: pl/pgSQL variable substitution
Next
From: Adarsh Sharma
Date:
Subject: Tablespace Issue