Skip site navigation (1) Skip section navigation (2)

Using a tablename as a parameter to a function.

From: Stephen Ingram <ingram(at)samsix(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Using a tablename as a parameter to a function.
Date: 2001-12-20 22:45:31
Message-ID: (view raw, whole thread or download thread mbox)
Lists: 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.



drop function testfunc( text ); 

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

        tablename         alias for $1; 


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

        select into rowcount count(*) from tablename;

        return rowcount;

language 'plpgsql';

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


pgsql-novice by date

Next:From: Pat MarchantDate: 2001-12-20 22:47:26
Subject: Re: Trigger question
Previous:From: Salvatore SferrazzaDate: 2001-12-20 22:24:14
Subject: Re: FW: Random Selection from TABLE

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group