delete from journal_ftp;
insert into journal_ftp(id,chi,quando,idp,ida,ora,data,idc) select * from dblink('dbname=pgare','select id,chi,quando,idp,ida,ora,data,idc from vjournal') AS journal(id integer,chi numeric,quando timestamp,idp integer,ida integer,ora text,data date,idc varchar);
insert into journal_figc(id_utente_modificatore,matricola_figc_sq_modificatrice,created_at,id_partita,id_azione,orario,data,matricola_impianto_figc,azione_annullata,id_old)
select
null as id_utente_modificatore,
null as matricola_figc_sq_modificatrice,
j.quando as created_at,
p.id as id_partita,
j.ida as id_azione,
j.ora::time as orario,
j.data as data,
idc as matricola_impianto_figc,
false as azione_annullata,j.id
from journal_ftp j,partite_figc p where j.idp=p.id_partita_ftp and j.id not in(select id_old from journal_figc) order by j.id;