From: Kevin Nikiforuk <Kevin.Nikiforuk@Acrodex.com>
To: pgsql-sql@postgresql.org
Subject: CREATE TABLE AS inside of a function
Date: Fri, 21 Jul 2006 13:14:29 -0600
Message-ID: <6EA72AC41F81D242A38AAF505523A94D022DF6@kappa.acrodex.net> (raw)
So now that I've got my loops working, on to my next newbie question. I've created my function and in it, I want to loop through the results of a select and for each value of my loop counter, I want to create a new table, but I can't figure out how to use a variable in the name of the new table, see below.
CREATE OR REPLACE FUNCTION rgio() RETURNS INTEGER as $$
DECLARE
lv RECORD;
BEGIN
FOR lv IN SELECT DISTINCT rg FROM ldevrg LOOP
CREATE TABLE rgio_$lv AS
SELECT ldev
FROM ldevrg
WHERE rg='$lv';
END LOOP;
RETURN 1;
END;
$$ LANGUAGE plpgsql;
Thanks,
Kevin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-sql@postgresql.org
Cc: Kevin.Nikiforuk@Acrodex.com
Subject: Re: CREATE TABLE AS inside of a function
In-Reply-To: <6EA72AC41F81D242A38AAF505523A94D022DF6@kappa.acrodex.net>
References: <6EA72AC41F81D242A38AAF505523A94D022DF6@kappa.acrodex.net>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox