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

From Jochem van Dieten
Subject PL/pgSQL: dynamic tablename
Date
Msg-id 3C4B31C7.7010004@oli.tudelft.nl
Whole thread Raw
Responses Re: PL/pgSQL: dynamic tablename  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
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


pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: frustration with database size
Next
From: alavoor
Date:
Subject: PostgreSQL Licence: GNU/GPL