X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id CB7039DCA20 for ; Wed, 1 Feb 2006 11:26:27 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 65471-08 for ; Wed, 1 Feb 2006 11:26:32 -0400 (AST) Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 4FC309DCA1F for ; Wed, 1 Feb 2006 11:26:25 -0400 (AST) Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.191]) by svr4.postgresql.org (Postfix) with ESMTP id B63855AF8D8 for ; Wed, 1 Feb 2006 15:26:23 +0000 (GMT) Received: from [212.227.126.202] (helo=mrvnet.kundenserver.de) by moutng.kundenserver.de with esmtp (Exim 3.35 #1) id 1F4Js8-0003Al-00 for pgsql-sql@postgresql.org; Wed, 01 Feb 2006 16:26:16 +0100 Received: from [172.23.4.28] (helo=pustefix161.kundenserver.de) by mrvnet.kundenserver.de with esmtp (Exim 3.35 #1) id 1F4Js8-0003LK-00 for pgsql-sql@postgresql.org; Wed, 01 Feb 2006 16:26:16 +0100 Message-Id: <15802822.125111138807576371.JavaMail.servlet@kundenserver> From: christian.michels@eifelgeist.com To: Subject: executing dynamic commands MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Priority: 3 X-Binford: 6100 (more power) X-Mailer: Webmail X-Originating-From: 28358181 X-Routing: DE X-Message-Id: <28358181$1138807576371172.23.4.285448445@pustefix161.kundenserver.de--1660662845> X-Received: from pustefix161.kundenserver.de by 212.232.252.130 with HTTP id 28358181 for [pgsql-sql@postgresql.org]; Wed, 1 Feb 2006 16:26:16 CET Date: Wed, 01 Feb 2006 16:26:16 +0100 X-Provags-ID: kundenserver.de abuse@kundenserver.de ident:@172.23.4.28 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.55 required=5 tests=[NO_REAL_NAME=0.55] X-Spam-Score: 0.55 X-Spam-Level: X-Archive-Number: 200602/2 X-Sequence-Number: 24012 Hi, I user PostgreSQl 8.0.4 on Win2003 Server and write a function to copy rows from one table into another table with the same column definition. My first approach was to use something like: query_value := 'INSERT INTO ' || tabledest || ' SELECT * FROM ' || tablesrc; EXECUTE query_value; This only works if the column definition AND the order between source and destination is the same ! In my case I have always the same column definitions but they are not in the same order between source and destination table. What I tryed then is to loop through the column definition of the source and query the sourcetable for the value. For that I have to execut a query with dynamic tablename and dynamic columname to generate two stings one with the columndefinitin and one with the columnvalues to exececute something like: INSERT INTO tabelfoo (columndefinitinstring) VALUES (columnvaluesstring) see snip of function: fieldvalues RECORD; output RECORD; insertvalues VARCHAR; fieldname VARCHAR; -- Get Attribute List from Table and write it to output -- Read Values of Fieldname from source query_value := 'select * from ' || tablesrc ; FOR fieldvalues IN EXECUTE query_value LOOP FOR output IN SELECT a.attnum, a.attname AS field, FROM pg_class c, pg_attribute a, pg_type t WHERE c.relname = tablesrc AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid ORDER BY a.attnum LOOP -- Read Field Name from Out Table fieldname := output.field; -- Write Field Name into Variable IF insertcolumns IS NULL THEN insertcolumns := fieldname; ELSE insertcolumns := insertcolumns || ',' || fieldname; END IF; Until here everyting is fine ... but now I try to query the value from RECORD fieldvalues with the columname fieldname variable from the inner loop ! I tryed the following ... query_value := 'select quote_ident(' || fieldvalues || ').quote_literal(' || fieldname ||')'; EXECUTE query_value; and I get the following error message ... ERROR: could not find array type for data type record CONTEXT: SQL statement "SELECT 'select quote_ident(' || $1 || ').quote_literal(' || $2 ||')'" PL/pgSQL function "prx_db__appendtable" line 87 at assignment END LOOP; END LOOP; I know the function is not runnable, but my question is how can I dynamically combine "fieldvalues"."fieldname" to read the values column by colum out if a RECORD variable to generate the "columnvaluesstring" mentioned above ?! Maybe this approach is to complicated and there is a quick and easy solution ?! Any help is very much appreciated !! Thanx a lot & Regards Chris