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 7CDCF9FA4C5 for ; Mon, 24 Jul 2006 17:53:32 -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 68351-02 for ; Mon, 24 Jul 2006 17:53:19 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from Saturn.acrodex.com (saturn.acrodex.com [139.142.184.3]) by postgresql.org (Postfix) with ESMTP id 359929FA441 for ; Mon, 24 Jul 2006 17:53:13 -0300 (ADT) X-MimeOLE: Produced By Microsoft Exchange V6.5.7226.0 Content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable Subject: Re: CREATE TABLE AS inside of a function Date: Mon, 24 Jul 2006 14:49:52 -0600 Message-ID: <6EA72AC41F81D242A38AAF505523A94D022DFC@kappa.acrodex.net> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: [SQL] CREATE TABLE AS inside of a function Thread-Index: AcatCWAuqaZV4uw4QUuytD3EcHZSTgCWVW9O From: "Kevin Nikiforuk" To: "Erik Jones" , "Rodrigo De Leon" Cc: X-imss-version: 2.041 X-imss-result: Passed X-imss-scores: Clean:99.90000 C:2 M:3 S:5 R:5 X-imss-settings: Baseline:1 C:1 M:1 S:1 R:1 (0.0000 0.0000) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.033 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200607/303 X-Sequence-Number: 25877 So, I've changed my code as Erik suggested: =20 CREATE OR REPLACE FUNCTION rgio() RETURNS INTEGER as $$ DECLARE lv RECORD; BEGIN FOR lv IN SELECT DISTINCT rg FROM ldevrg LOOP =20 EXECUTE 'CREATE TABLE rgio_' || $lv || ' AS SELECT ldev FROM ldevrg WHERE rg=3D' || quote_literal($lv) || ';' =20 END LOOP; RETURN 1; END; $$ LANGUAGE plpgsql; And I get: =20 psql:rgio.sql:32: ERROR: syntax error at or near "$" at character 33 QUERY: SELECT 'CREATE TABLE rgio_' || $ $1 || ' AS SELECT ldev FROM ldevrg WHERE rg=3D' || quote_literal($ $2 ) || ';' END = LOOP CONTEXT: SQL statement in PL/PgSQL function "rgio" near line 23 psql:rgio.sql:32: LINE 1: SELECT 'CREATE TABLE rgio_' || $ $1 || ' AS psql:rgio.sql:32: = ^ ________________________________ From: pgsql-sql-owner@postgresql.org on behalf of Erik Jones Sent: Fri 7/21/2006 3:04 PM To: Rodrigo De Leon Cc: pgsql-sql@postgresql.org Subject: Re: [SQL] CREATE TABLE AS inside of a function 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=3D'$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=3D\'' || $lv || '\';' -- this line could have also been:=20 WHERE rg=3D' || 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) ---------------------------(end of broadcast)--------------------------- TIP 1: if posting/reading through Usenet, please send an appropriate subscribe-nomail command to majordomo@postgresql.org so that your message can get through to the mailing list cleanly