Re: table name in pl/pgsql

From: Richard Huxton <dev(at)archonet(dot)com>
To: Adam Witney <awitney(at)sghms(dot)ac(dot)uk>
Cc: "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:25:26
Message-ID: 41A5F966.3070702@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Adam Witney wrote:
> 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;

That won't work either, you'll need to user FOR..IN..EXECUTE:

CREATE TABLE exectest (a integer, b text, PRIMARY KEY (a));
COPY exectest FROM stdin;
1 aaa
2 bbb
3 ccc
\.

CREATE FUNCTION demo_exec_fn() RETURNS boolean AS '
DECLARE
r RECORD;
BEGIN
FOR r IN EXECUTE ''SELECT * FROM exectest''
LOOP
RAISE NOTICE ''a=%, b=%'', r.a, r.b;
END LOOP;
RETURN true;
END
' LANGUAGE plpgsql;

SELECT demo_exec_fn();

--
Richard Huxton
Archonet Ltd

In response to

Browse pgsql-general by date

  From Date Subject
Next Message CoL 2004-11-25 15:30:07 Re: table name in pl/pgsql
Previous Message Adam Witney 2004-11-25 15:07:02 Re: table name in pl/pgsql