Re: executing dynamic commands

From: christian(dot)michels(at)eifelgeist(dot)com
To: <sszabo(at)megazone(dot)bigpanda(dot)com>
Cc: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: executing dynamic commands
Date: 2006-02-02 08:49:42
Message-ID: 20393697.175191138870182592.JavaMail.servlet@kundenserver
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Thanx a lot guys - it works !

Cheers

Chris

On Wed, 1 Feb 2006 christian ( dot ) michels ( at ) eifelgeist ( dot ) 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 <reordered columns to match dest order> FROM
tablesrc

Browse pgsql-sql by date

  From Date Subject
Next Message Markus Schaber 2006-02-02 14:16:45 Re: Does PostgreSQL support job?
Previous Message Magnus Hagander 2006-02-02 08:49:32 Re: Does PostgreSQL support job?