pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Need a script that bakes INSERT script from SELECT results
2+ messages / 2 participants
[nested] [flat]

* Need a script that bakes INSERT script from SELECT results
@ 2009-04-16 11:29  ShuA <noface@inbox.ru>
  0 siblings, 1 reply; 2+ messages in thread

From: ShuA @ 2009-04-16 11:29 UTC (permalink / raw)
  To: pgsql-sql

I'm a novice to PL/pgSQL, and I'm curious, how to write function, that wraps SELECT results into INSERT script. 

The functionality I need is about to replicate table data into remote DB.

I have tried dblink_build_insert(), but it requires primary key on tuple to create INSERT script from. I would prefer filter clause to primary key, to generate either multiple inserts or one multi-row insert.

Could someone post an example how to LOOP through row fields to wrap them into 'VALUES(...,,)' list?

Thanks,
Olksy




^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Need a script that bakes INSERT script from SELECT results
@ 2009-04-16 12:25  John DeSoi <desoi@pgedit.com>
  parent: ShuA <noface@inbox.ru>
  0 siblings, 0 replies; 2+ messages in thread

From: John DeSoi @ 2009-04-16 12:25 UTC (permalink / raw)
  To: ShuA <noface@inbox.ru>; +Cc: pgsql-sql


On Apr 16, 2009, at 7:29 AM, ShuA wrote:

> Could someone post an example how to LOOP through row fields to wrap  
> them into 'VALUES(...,,)' list?


If you declare record or table row types, you can insert the values  
using (rec.*), something like this:


create or replace function test ()
returns void as $$
declare
	rec record;
begin
	for rec in select * from whatever loop
		insert into some_table values (rec.*);
	end loop;
end;
$$ language plpgsql;






John DeSoi, Ph.D.







^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2009-04-16 12:25 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2009-04-16 11:29 Need a script that bakes INSERT script from SELECT results ShuA <noface@inbox.ru>
2009-04-16 12:25 ` John DeSoi <desoi@pgedit.com>

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