Re: function with tablename parameter

From: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
To: Miso Hlavac <hlavki(at)medium13(dot)sk>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: function with tablename parameter
Date: 2003-10-27 16:05:14
Message-ID: 20031027080310.B29441@megazone.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, 27 Oct 2003, Miso Hlavac wrote:

> hello,
>
> it is possible to write something similar???
>
> create function get_count(varchar(32)) RETURNS int4 AS '
> DECLARE
> tmp int4;
> BEGIN
> SELECT COUNT(*) INTO tmp FROM $1;
> RETURN tmp;
> END;' LANGUAGE 'plpgsql';

Youll need to do something a little more complicated like:

create function get_count(varchar) RETURNS int8 AS '
DECLARE
tmp record;
BEGIN
FOR tmp IN EXECUTE ''SELECT COUNT(*) AS count FROM '' || $1 LOOP
RETURN tmp.count;
END LOOP;
END;'
LANGUAGE 'plpgsql';

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Chris M. Gamble 2003-10-27 16:10:02 Replication again
Previous Message Adam Witney 2003-10-27 15:53:36 Re: table functions + user defined types