executing dynamic commands - Mailing list pgsql-sql

From christian.michels@eifelgeist.com
Subject executing dynamic commands
Date
Msg-id 15802822.125111138807576371.JavaMail.servlet@kundenserver
Whole thread Raw
Responses Re: executing dynamic commands  (Stephan Szabo <sszabo@megazone.bigpanda.com>)
List pgsql-sql
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
samecolumn 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
thatI have to execut a query with dynamic tablename and dynamic columname to generate two stings one with the
columndefinitinand 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.attnumLOOP         -- 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
variablefrom 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
readthe 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


pgsql-sql by date:

Previous
From: Achilleus Mantzios
Date:
Subject: Re: how is searchable email archive on
Next
From: "codeWarrior"
Date:
Subject: Re: executing dynamic commands