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 9E2479DC809 for ; Wed, 1 Feb 2006 13:10:37 -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 90165-02 for ; Wed, 1 Feb 2006 13:10:36 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from megazone.bigpanda.com (megazone.bigpanda.com [64.147.171.210]) by postgresql.org (Postfix) with ESMTP id 7CE099DC949 for ; Wed, 1 Feb 2006 13:10:35 -0400 (AST) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 0D83336266; Wed, 1 Feb 2006 09:05:52 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 0BAEF3624F; Wed, 1 Feb 2006 09:05:52 -0800 (PST) Date: Wed, 1 Feb 2006 09:05:51 -0800 (PST) From: Stephan Szabo To: christian.michels@eifelgeist.com Cc: pgsql-sql@postgresql.org Subject: Re: executing dynamic commands In-Reply-To: <15802822.125111138807576371.JavaMail.servlet@kundenserver> Message-ID: <20060201090406.S89604@megazone.bigpanda.com> References: <15802822.125111138807576371.JavaMail.servlet@kundenserver> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.145 required=5 tests=[AWL=0.145] X-Spam-Score: 0.145 X-Spam-Level: X-Archive-Number: 200602/7 X-Sequence-Number: 24017 On Wed, 1 Feb 2006 christian.michels@eifelgeist.com wrote: > 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) You might have better luck with a INSERT ... SELECT where you've reordered the columns in the select list INSERT INTO tabledest SELECT FROM tablesrc