pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Stephan Szabo <sszabo@megazone.bigpanda.com>
To: christian.michels@eifelgeist.com
Cc: pgsql-sql@postgresql.org
Subject: Re: executing dynamic commands
Date: Wed, 1 Feb 2006 09:05:51 -0800 (PST)
Message-ID: <20060201090406.S89604@megazone.bigpanda.com> (raw)
In-Reply-To: <15802822.125111138807576371.JavaMail.servlet@kundenserver>
References: <15802822.125111138807576371.JavaMail.servlet@kundenserver>

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 <reordered columns to match dest order> FROM
tablesrc



view thread (6+ messages)  latest in thread

Message-ID: <20060201090406.S89604@megazone.bigpanda.com>
Permalink:  ../20060201090406.S89604@megazone.bigpanda.com/
Also on:    postgresql.org/message-id/20060201090406.S89604@megazone.bigpanda.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: sszabo@megazone.bigpanda.com, christian.michels@eifelgeist.com
  Subject: Re: executing dynamic commands
  In-Reply-To: <20060201090406.S89604@megazone.bigpanda.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox