X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 3C93C9FA6B1 for ; Fri, 21 Jul 2006 18:04:47 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 68645-05 for ; Fri, 21 Jul 2006 18:04:40 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from myemma.com (db1.e2ma.net [66.179.147.163]) by postgresql.org (Postfix) with ESMTP id 7DCC29FA6A2 for ; Fri, 21 Jul 2006 18:04:40 -0300 (ADT) Received: from localhost (localhost.localdomain [127.0.0.1]) by myemma.com (Postfix) with ESMTP id 6149423CCA; Fri, 21 Jul 2006 16:07:10 -0500 (CDT) Received: from myemma.com ([127.0.0.1]) by localhost (velma [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 22127-07; Fri, 21 Jul 2006 16:07:10 -0500 (CDT) Received: from [10.0.23.15] (65.107.156.170.ptr.us.xo.net [65.107.156.170]) by myemma.com (Postfix) with ESMTP id 003C323CD0; Fri, 21 Jul 2006 16:07:09 -0500 (CDT) Message-ID: <44C14165.2040304@myemma.com> Date: Fri, 21 Jul 2006 16:04:37 -0500 From: Erik Jones User-Agent: Thunderbird 1.5.0.4 (Windows/20060516) MIME-Version: 1.0 To: Rodrigo De Leon Cc: pgsql-sql@postgresql.org Subject: Re: CREATE TABLE AS inside of a function References: <6EA72AC41F81D242A38AAF505523A94D022DF6@kappa.acrodex.net> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at myemma.com X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.596 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO, SPF_HELO_PASS, SPF_PASS X-Spam-Level: X-Archive-Number: 200607/284 X-Sequence-Number: 25858 Rodrigo De Leon wrote: > On 7/21/06, Kevin Nikiforuk 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 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 > > See: > http://www.postgresql.org/docs/8.1/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN > Also, I really recommend enough that you read chapters 32. Extending SQL, 33. Triggers, and 36. Pl/pgSQL in their entirety. In fact, to keep up with the linking to them for you: http://www.postgresql.org/docs/8.1/interactive/extend.html http://www.postgresql.org/docs/8.1/interactive/triggers.html http://www.postgresql.org/docs/8.1/interactive/plpgsql.html And, so that I don't feel like I'm becoming one of those rtfm jerks I always complain about: what you need to do is place your CREATE TABLE statement in an EXECUTE directive like so (inside the the FOR body): EXECUTE 'CREATE TABLE rgio_' || $lv || ' AS SELECT ldev FROM ldevrg WHERE rg=\'' || $lv || '\';' -- this line could have also been: WHERE rg=' || quote_literal($lv) || ';' EXECUTE takes a query in a string to execute and you need to use string concatenation to build the string if you're using variables from the function in the query. Pl/pgSQL doesn't have any variable substitution inside of strings (like in double quoted string in PHP) which is why you need to use the concatenation bit. -- erik jones software development emma(r)