CREATE TABLE AS inside of a function

From: "Kevin Nikiforuk" <Kevin(dot)Nikiforuk(at)Acrodex(dot)com>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: CREATE TABLE AS inside of a function
Date: 2006-07-21 19:14:29
Message-ID: 6EA72AC41F81D242A38AAF505523A94D022DF6@kappa.acrodex.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Rodrigo De Leon 2006-07-21 20:32:07 Re: CREATE TABLE AS inside of a function
Previous Message Scott Marlowe 2006-07-21 16:27:04 Re: System catalog table privileges