Re: table name as variable within Function

Lists: pgsql-general
From: Clark Allan <clarka(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: table name as variable within Function
Date: 2005-07-07 18:22:38
Message-ID: 4a7a732105070711222daafaf4@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Here is an example of what i would like to do...
---------------------------------------
CREATE FUNCTION fun_totalrecords(varchar) RETURNS int8 AS'
DECLARE
theTable ALIAS FOR $1;
result int := 0;
BEGIN
result = COUNT(*) FROM theTable; -- this is where i need help
RETURN result;

END; 'LANGUAGE 'plpgsql';
---------------------------------------
Thanks for the help....
Clark


From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Clark Allan <clarka(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: table name as variable within Function
Date: 2005-07-07 20:41:09
Message-ID: 20050707204109.GA26949@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Thu, Jul 07, 2005 at 12:22:38PM -0600, Clark Allan wrote:
> Here is an example of what i would like to do...
> ---------------------------------------
> CREATE FUNCTION fun_totalrecords(varchar) RETURNS int8 AS'
> DECLARE
> theTable ALIAS FOR $1;
> result int := 0;
> BEGIN
> result = COUNT(*) FROM theTable; -- this is where i need help
> RETURN result;
>
> END; 'LANGUAGE 'plpgsql';

See "Executing Dynamic Commands" in the PL/pgSQL documentation:

http://www.postgresql.org/docs/8.0/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/