Re: Table name as parameter in function

From: Timothy Perrigo <tperrigo(at)wernervas(dot)com>
To: PgSQL General List <pgsql-general(at)postgresql(dot)org>
Subject: Re: Table name as parameter in function
Date: 2004-11-23 21:16:01
Message-ID: E0B75230-3D94-11D9-8D91-000A95C4F0A2@wernervas.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

You'll need to use the EXECUTE command to build the SQL dynamically.

See:
http://www.postgresql.org/docs/7.4/interactive/plpgsql-
statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

On Nov 23, 2004, at 2:56 PM, Alexander Pucher wrote:

> Hi,
>
> struggling around with this for some time:
>
> How can I use a table name as a parameter in a PL/pgSQL function ??
>
> I tried this but it didn't work...
>
>
> CREATE OR REPLACE FUNCTION my_row_count(text) RETURNS int4 AS '
> DECLARE
> num_rows int4;
> BEGIN
> num_rows := (select count(*) from $1);
> RETURN num_rows;
> END;
> ' LANGUAGE plpgsql;
>
> Thnaks for any input!
>
> regards,
> alex.
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to
> majordomo(at)postgresql(dot)org
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ennio-Sr 2004-11-23 21:16:52 Re: Can you help solve restore problem?
Previous Message Gary L. Burnore 2004-11-23 21:12:11 Re: Upcoming Changes to News Server ...