X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (wm.hub.org [200.46.204.128]) by postgresql.org (Postfix) with ESMTP id 0C2539FABE7 for ; Fri, 21 Jul 2006 17:32:13 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.128]) (amavisd-new, port 10024) with ESMTP id 16370-03 for ; Fri, 21 Jul 2006 20:32:10 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from ug-out-1314.google.com (ug-out-1314.google.com [66.249.92.171]) by postgresql.org (Postfix) with ESMTP id 80D3B9FAA6E for ; Fri, 21 Jul 2006 17:32:09 -0300 (ADT) Received: by ug-out-1314.google.com with SMTP id m3so1444285ugc for ; Fri, 21 Jul 2006 13:32:07 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=CSq728UGKSm+VOuJquYKCgao9POx9xEjSsEAyaZmutsnX03gUIY8uDD3Je4Iepv6Cl0b3yquxic56N53xpJ9DdgfgxY+SpJD2YAs0i3pgfilNHnrTM1GzRimHMFv9D4L1SJWnmsxLITo22pUCt/Q43FjJbrsVaB/FF89dUP10B4= Received: by 10.67.100.17 with SMTP id c17mr1067458ugm; Fri, 21 Jul 2006 13:32:07 -0700 (PDT) Received: by 10.66.234.13 with HTTP; Fri, 21 Jul 2006 13:32:07 -0700 (PDT) Message-ID: Date: Fri, 21 Jul 2006 14:32:07 -0600 From: "Rodrigo De Leon" To: pgsql-sql@postgresql.org Subject: Re: CREATE TABLE AS inside of a function In-Reply-To: <6EA72AC41F81D242A38AAF505523A94D022DF6@kappa.acrodex.net> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <6EA72AC41F81D242A38AAF505523A94D022DF6@kappa.acrodex.net> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.199 tagged_above=0 required=5 tests=DNS_FROM_RFC_ABUSE, SPF_PASS, UPPERCASE_25_50 X-Spam-Level: X-Archive-Number: 200607/283 X-Sequence-Number: 25857 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 Regards, Rodrigo