Re: PL/pgSQL: dynamic tablename - Mailing list pgsql-general

From Steve Boyle \(Roselink\)
Subject Re: PL/pgSQL: dynamic tablename
Date
Msg-id 001e01c1a1fc$1496c420$c55869d5@dualtower
Whole thread Raw
In response to PL/pgSQL: dynamic tablename  (Jochem van Dieten <jochemd@oli.tudelft.nl>)
List pgsql-general
Jochem,

You will need to use EXECUTE [sql code] from within your function if your
using dynamic sql.  For reasons why + examples please see:

http://developer.postgresql.org/docs/postgres/plpgsql-statements.html#PLPGSQ
L-STATEMENTS-EXECUTING-DYN-QUERIES

hih

steve boyle


----- Original Message -----
From: "Jochem van Dieten" <jochemd@oli.tudelft.nl>
To: <pgsql-general@postgresql.org>
Sent: Sunday, January 20, 2002 9:08 PM
Subject: [GENERAL] PL/pgSQL: dynamic tablename


> I am building a set of functions to manage some trees. I would like to
> use a dynamic tablename. Something like:
>
> CREATE FUNCTION fn_test(varchar, integer, varchar, varchar) RETURNS
> INTEGER AS '
> DECLARE
> beforenode INT4;
> newid INT4;
> BEGIN
> SELECT INTO beforenode lft
> FROM $4
> WHERE ID = $2;
>
> More processing
>
> RETURN newid;
> END;
> ' LANGUAGE 'plpgsql';
>
> Problem is that I always get an error about $4 being used incorrectly.
> If I hardcode the name of the table it works just fine.
>
> Does anybody have any suggestion on how to use a dynamic tablename
> passed as an attribute in a function?
>
> Jochem
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/users-lounge/docs/faq.html
>


pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: PL/pgSQL: dynamic tablename
Next
From: Jeff Eckermann
Date:
Subject: Re: Questions for the COPY expert