Re: table name in pl/pgsql

From: Adam Witney <awitney(at)sghms(dot)ac(dot)uk>
To: "ON(dot)KG" <skyer(at)on(dot)kg>, pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: table name in pl/pgsql
Date: 2004-11-25 15:07:02
Message-ID: BDCBA596.3C7FB%awitney@sghms.ac.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


I think you would have to do it something like this, although whether the
SELECT INTO works in an EXECUTE context I am not sure (note, completely
untested code!)

CREATE FUNCTION get_count(text, text) RETURNS int2 AS '
DECLARE
cnt int4;
BEGIN

EXECUTE ''SELECT INTO cnt COUNT(*) FROM table_'' || $1 || '' WHERE key =
'' || $2;

RETURN cnt;
END;'
LANGUAGE 'plpgsql';

> New question:
>
> i have tables like
> table_20041124,
> table_20041125,
> etc...
>
> i'm trying to make function (for example):
> =====================================
> CREATE FUNCTION get_count(text, text)
> RETURNS int2 AS '
> DECLARE
> cnt int4;
> BEGIN
> SELECT INTO cnt COUNT(*)
> FROM table_$1 -- That doesn't work
> WHERE key = $2;
>
> RETURN cnt;
> END;'
> LANGUAGE 'plpgsql';
> =====================================
>
> call this function by:
>
> =====================================
> SELECT get_count("20041124", "something");
> =====================================
>
> string in funstion - FROM table_$1
>
> how could i get a final correct table name here?
>
> Thanx!
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://archives.postgresql.org

--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Richard Huxton 2004-11-25 15:25:26 Re: table name in pl/pgsql
Previous Message ON.KG 2004-11-25 15:02:40 Re: Trigger before insert