X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id BC0DA9FA509 for ; Mon, 1 May 2006 01:49:59 -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 86159-01 for ; Mon, 1 May 2006 01:49:50 -0300 (ADT) X-Greylist: delayed 03:19:54.411977 by SQLgrey- Received: from tamu-relay.tamu.edu (smtp-relay.tamu.edu [165.91.22.120]) by postgresql.org (Postfix) with ESMTP id 55D1D9FA4B3 for ; Mon, 1 May 2006 01:49:50 -0300 (ADT) Received: from coe.tamu.edu (edsun.coe.tamu.edu [165.91.232.6]) by tamu-relay.tamu.edu (8.13.4/8.13.3) with ESMTP id k414nkN5067474; Sun, 30 Apr 2006 23:49:47 -0500 (CDT) (envelope-from bkim@coe.tamu.edu) Received: from coe.tamu.edu (coe.tamu.edu [165.91.232.6]) by coe.tamu.edu (8.11.7p2+Sun/8.11.7) with ESMTP id k414nj428092; Sun, 30 Apr 2006 23:49:45 -0500 (CDT) Date: Sun, 30 Apr 2006 23:49:45 -0500 (CDT) From: "Ben K." To: "Gregory S. Williamson" cc: pgsql-sql@postgresql.org Subject: Re: Linked List In-Reply-To: <71E37EF6B7DCC1499CEA0316A2568328024BC3D4@loki.wc.globexplorer.net> Message-ID: References: <71E37EF6B7DCC1499CEA0316A2568328024BC3D4@loki.wc.globexplorer.net> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed Received-SPF: pass (smtp-relay.tamu.edu: 165.91.232.6 is authenticated by a trusted mechanism) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200605/1 X-Sequence-Number: 25032 > The pgsql function is compiled and wouldn't know how to handle a table >name as a variable. > If you rewrite the SQL to use the 'EXECUTE' statement I think you could >do this, something along the lines of (untested): > EXECUTE ''INSERT INTO '' || quote_ident(tmplist) || '' (SELECT * FROM >links WHERE p=x)''; Thanks. Yet, if I give the table name as the argument, I get the same error. drop function traverse(integer, text); create or replace function traverse (integer, text) returns integer as $$ declare x int; tname alias for $2; begin x := $1; while x is not null loop select n into x from links where p = x; insert into tmplink (select * from links where p=x); EXECUTE ''INSERT INTO '' || quote_ident(tname) || '' (SELECT * FROM links WHERE p=x)''; end loop; return 1 ; end; $$ language plpgsql; The above gives the following error. Please note that the first and second args are mixed up now. I tested it only on 8.0 (FC 4) and didn't on 8.1 and wonder whether it's a bug or I may be doing something wrong. Using tname or $2 doesn't change the result. ======================================================================== # select traverse(0, 'links2'); ERROR: syntax error at or near "INSERT" at character 11 QUERY: SELECT ''INSERT INTO '' || quote_ident( $1 ) || '' (SELECT * FROM links WHERE p= $2 )'' CONTEXT: PL/pgSQL function "traverse" line 10 at execute statement LINE 1: SELECT ''INSERT INTO '' || quote_ident( $1 ) || '' (SELECT ... ======================================================================== Regards, Ben K. Developer http://benix.tamu.edu