Re: CREATE TABLE AS inside of a function - Mailing list pgsql-sql

From Rodrigo De Leon
Subject Re: CREATE TABLE AS inside of a function
Date
Msg-id a55915760607211332ye89afd4m9bb2ad456fcef99b@mail.gmail.com
Whole thread Raw
In response to CREATE TABLE AS inside of a function  ("Kevin Nikiforuk" <Kevin.Nikiforuk@Acrodex.com>)
Responses Re: CREATE TABLE AS inside of a function
List pgsql-sql
On 7/21/06, Kevin Nikiforuk <Kevin.Nikiforuk@acrodex.com> wrote:
> 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
loopthrough the results of a select and for each value of my loop counter, I want to create a new table, but I can't
figureout 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

See:
http://www.postgresql.org/docs/8.1/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

Regards,

Rodrigo


pgsql-sql by date:

Previous
From: "Kevin Nikiforuk"
Date:
Subject: CREATE TABLE AS inside of a function
Next
From: Erik Jones
Date:
Subject: Re: CREATE TABLE AS inside of a function