0

I need to insert some info I'm reading from a database into a table that belongs to a different one.

I have my info in a RECORD variable, let's call it my_row.

Obviously when I try

SELECT dblink_exec('INSERT INTO remote_table VALUES (my_row.*)');

it doesn't recognize who my_row is.

What other options do I have?

4
  • Not sure if * is supported in insert, either way it's bad habit. Try insert into (col1,col2) select x,y from my_row; Commented Jan 28, 2013 at 13:39
  • It is supported in insert, I run an example earlier (same database, without the dblink) and it did work. I totally agree with what you say about it being a bad habit, and I generally don't do it, but in this case and for exemplification reasons (my full table has about 16 columns) I think it's okay. I promise to change it in the final code :) Commented Jan 28, 2013 at 13:51
  • What version of Postgre are you on? Commented Jan 29, 2013 at 13:14
  • The version I'm using is 9.1 Commented Jan 29, 2013 at 17:42

2 Answers 2

1

Here's one option:

CREATE TABLE junk(a text, b text,c TIMESTAMP, d hstore);

DO
$BODY$
DECLARE 
RecordVar junk%rowtype;
SqlCommand TEXT:= 'INSERT INTO junk(a,b,c,d) VALUES(';
BEGIN
   RecordVar.a = 'junk''''a';
   RecordVar.b = 'junkb';
   RecordVar.c = NOW();
   RecordVar.d = '"key1"=>"value1"';
   SqlCommand := SqlCommand  || '''' || RecordVar.a || '''' || ',' || '''' || RecordVar.b || '''' || ',' ||'''' || RecordVar.c || '''' || ',' ||'''' || RecordVar.d || '''' || ');';

   RAISE NOTICE 'SQL = %',SqlCommand;

   PERFORM dblink('dblink_logging',SqlCommand);

END;
$BODY$

    Select a,b,c from junk;
    junk'a|junkb|2013-01-28 09:53:22.308|"key1"=>"value1"
    DROP TABLE junk;
Sign up to request clarification or add additional context in comments.

5 Comments

This would be a great solution if my data didn't include timstamps and strings with plenty of apostrophes :( I am hoping for a solution that would allow me to send other types of objects besides string. Although admittedly I am very not certain there is one.
See if this works for you I updated to include text with ' and a column with a timestamp.
It does work better, thank you. Now it seems I only have problems with the few columns that are type hstore.
Updated to include a hstore column does this give you what you need?
In theory yes, it should solve my problem, but I've already tried doing it this way and in my case it throws a "failed to find conversion function from unknown to text" error.
1

Try something like:

SELECT dblink_exec('INSERT INTO remote_table VALUES '|| quote_literal(my_row.*));

The idea - to use dynamic SQL. To convert the row to its text representation, escape special chars and form the INSERT statement.

2 Comments

In my opinion not naming the target columns and source columns is lazy and is bad habit.
Sadly, this doesn't work as it won't properly format the output -- ie won't put apostrophes around the strings. And also I worry about problems that may show up when changing the timestamps to string and back.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.