Using a tablename as a parameter to a function. - Mailing list pgsql-novice

From Stephen Ingram
Subject Using a tablename as a parameter to a function.
Date
Msg-id 20011220225409.XLBC18615.femail31.sdc1.sfba.home.com@there
Whole thread Raw
Responses Re: Using a tablename as a parameter to a function.
List pgsql-novice

Hi there,

Is it possible (legal syntax, I mean) to pass a tablename
as a parameter to a function?

I don't know if its impossible or just that my plpgsql code is
not up to scratch.


Don't laugh, I'm just starting!  :)

Thanks for any pointers anyone can give.

steve


=======================================================================


drop function testfunc( text );

create function testfunc( text ) returns int
as
'
    declare
        rowcount          int         := 0;

        tablename         alias for $1;

    begin

        -- ----------------------------------------------------------
        -- Report on the number of entries in the table.
        -- ----------------------------------------------------------

        select into rowcount count(*) from tablename;

        return rowcount;

    end;
'
language 'plpgsql';

select testfunc( 'MyTable' );
select testfunc( 'ATable' );
select testfunc( 'BigTable' );

pgsql-novice by date:

Previous
From: Salvatore Sferrazza
Date:
Subject: Re: FW: Random Selection from TABLE
Next
From: Pat Marchant
Date:
Subject: Trigger question