CREATE OR REPLACE FUNCTION food600.synchronizacia(
	)
    RETURNS void
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE
    
AS $BODY$
DECLARE
zobrazuj SMALLINT DEFAULT 1;
vysledok CHARACTER VARYING;
mip CHARACTER VARYING;
mport CHARACTER VARYING;
mdatabaza CHARACTER VARYING;
mmeno CHARACTER VARYING;
mheslo CHARACTER VARYING;
mipja CHARACTER VARYING;
mportja CHARACTER VARYING;
mdatabazaja CHARACTER VARYING;
mmenoja CHARACTER VARYING;
mhesloja CHARACTER VARYING;
mp1 CHARACTER VARYING;
mp2 CHARACTER VARYING;
mp3 CHARACTER VARYING;
mp4 CHARACTER VARYING;
mp5 CHARACTER VARYING;
mp6 CHARACTER VARYING;
mp7 CHARACTER VARYING;
mp8 CHARACTER VARYING;
mnazev CHARACTER VARYING;
mtabulka CHARACTER VARYING;
mdruh CHARACTER VARYING;
mprikaz CHARACTER VARYING;
mprikaz1 CHARACTER VARYING;
mobrazok BYTEA;
mporadie INTEGER;
midr BIGINT;
mojeid INTEGER;
mojmaster INTEGER;
midlocaldelete BIGINT;
midremotedelete BIGINT;
midlocalupdate BIGINT;
midremoteupdate BIGINT;
midlocalinsert BIGINT;
midremoteinsert BIGINT;
midlocalupdatekarty BIGINT;
midremoteupdatekarty BIGINT;
midlocalinsertkarty BIGINT;
midremoteinsertkarty BIGINT;
midlocalupdateskupiny BIGINT;
midremoteupdateskupiny BIGINT;
midlocalinsertskupiny BIGINT;
midremoteinsertskupiny BIGINT;
---- L.L. (10.12.2021)  Samostana tabulka hkredobr, pretoze 1. hkredobr uz obsahoval id_sync, ktore sa inak pouzivalo a 2. nestacilo %M1-M6%
midlocalupdatehkredobr BIGINT;
midremoteupdatehkredobr BIGINT;
midlocalinserthkredobr BIGINT;
midremoteinserthkredobr BIGINT;
midlocaldeletehkredobr BIGINT;
midremotedeletehkredobr BIGINT;
---- L.L. (10.12.2021)  koniec: Samostana tabulka hkredobr, pretoze 1. hkredobr uz obsahoval id_sync, ktore sa inak pouzivalo a 2. nestacilo %M1-M6%
midriadok BIGINT;
midriadokmaster BIGINT;
mdone INTEGER;
mckarty INTEGER;
mckartymaster INTEGER;
mcdruh INTEGER;
mcdruhmaster INTEGER;
nahrada CHARACTER VARYING;
pokracuj SMALLINT DEFAULT 1;
midmaster CHARACTER(2);

zoznaminsertovkarty CURSOR FOR SELECT idriadok, prikaz, c_karty, nazev, p1, p2, p3, p4, p5, p6  FROM food600.sync_karty WHERE operacia='I' AND idriadok>midlocalinsertkarty AND is_master=0 ORDER BY idriadok;
zoznamupdatovkarty CURSOR FOR SELECT idriadok, prikaz, c_karty, nazev, p1, p2, p3, p4, p5, p6 FROM food600.sync_karty WHERE operacia='U' AND idriadok>midlocalupdatekarty AND is_master=0 ORDER BY idriadok;
zoznamimportovkarty CURSOR FOR SELECT idriadok, prikaz, c_karty, nazev, p1, p2, p3, p4, p5, p6 FROM (SELECT * FROM dblink('connstrx','SELECT idriadok, prikaz, c_karty, nazev, p1, p2, p3, p4, p5, p6 FROM food600.sync_karty WHERE operacia=''I'' AND is_master>0 AND idriadok>'||(midremoteinsertkarty::CHARACTER(10))||' AND id_sync<>'||(mojeid::CHARACTER(10))||' ORDER BY idriadok') as remzmeny(idriadok bigint, prikaz character varying, c_karty integer, nazev character(30), p1 character varying, p2 character varying, p3 character varying, p4 character varying, p5 character varying, p6 character varying)) aaa;
zoznamremupdatovkarty CURSOR FOR SELECT idriadok, prikaz, c_karty, nazev, p1, p2, p3, p4, p5, p6 FROM (SELECT * FROM dblink('connstrx','SELECT idriadok, prikaz, c_karty, nazev, p1, p2, p3, p4, p5, p6 FROM food600.sync_karty WHERE operacia=''U'' AND is_master>0 AND idriadok>'||(midremoteupdatekarty::CHARACTER(10))||' AND id_sync<>'||(mojeid::CHARACTER(10))||' ORDER BY idriadok') as remzmeny(idriadok bigint, prikaz character varying, c_karty integer, nazev character(30), p1 character varying, p2 character varying, p3 character varying, p4 character varying, p5 character varying, p6 character varying)) aaa;
zoznaminsertovskupiny CURSOR FOR SELECT idriadok, prikaz, c_druh, druh, p1, p2, p3, p4, p5, p6  FROM food600.sync_skupiny WHERE operacia='I' AND idriadok>midlocalinsertskupiny AND is_master=0 ORDER BY idriadok;
zoznamupdatovskupiny CURSOR FOR SELECT idriadok, prikaz, c_druh, druh, p1, p2, p3, p4, p5, p6 FROM food600.sync_skupiny WHERE (operacia='U' OR operacia='D') AND idriadok>midlocalupdateskupiny AND is_master=0 ORDER BY idriadok;
zoznamimportovskupiny CURSOR FOR SELECT idriadok, prikaz, c_druh, druh, p1, p2, p3, p4, p5, p6 FROM (SELECT * FROM dblink('connstrx','SELECT idriadok, prikaz, c_druh, druh, p1, p2, p3, p4, p5, p6 FROM food600.sync_skupiny WHERE operacia=''I'' AND is_master>0 AND idriadok>'||(midremoteinsertskupiny::CHARACTER(10))||' AND id_sync<>'||(mojeid::CHARACTER(10))||' ORDER BY idriadok') as remzmeny(idriadok bigint, prikaz character varying, c_druh integer, druh character(10), p1 character varying, p2 character varying, p3 character varying, p4 character varying, p5 character varying, p6 character varying)) aaa;
zoznamremupdatovskupiny CURSOR FOR SELECT idriadok, prikaz, c_druh, druh, p1, p2, p3, p4, p5, p6 FROM (SELECT * FROM dblink('connstrx','SELECT idriadok, prikaz, c_druh, druh, p1, p2, p3, p4, p5, p6 FROM food600.sync_skupiny WHERE (operacia=''U'' OR operacia=''D'') AND is_master>0 AND idriadok>'||(midremoteupdateskupiny::CHARACTER(10))||' AND id_sync<>'||(mojeid::CHARACTER(10))||' ORDER BY idriadok') as remzmeny(idriadok bigint, prikaz character varying, c_druh integer, druh character(10), p1 character varying, p2 character varying, p3 character varying, p4 character varying, p5 character varying, p6 character varying)) aaa;

zoznaminsertovostatne CURSOR FOR SELECT idr, prikaz, idriadok, tabulka, p1, p2, p3, p4, p5, p6, poradie, nazev  FROM food600.sync_idriadok WHERE operacia='I' AND idr>midlocalinsert AND is_master=0 ORDER BY poradie DESC, idr;
zoznamupdatovostatne CURSOR FOR SELECT idr, prikaz, idriadok, tabulka, p1, p2, p3, p4, p5, p6, poradie, nazev FROM food600.sync_idriadok WHERE operacia='U' AND idr>midlocalupdate AND is_master=0 ORDER BY poradie DESC, idr;
zoznamimportovostatne CURSOR FOR SELECT idr, prikaz, idriadok, tabulka, p1, p2, p3, p4, p5, p6, poradie, nazev FROM (SELECT * FROM dblink('connstrx','SELECT idr, prikaz, idriadok, tabulka, p1, p2, p3, p4, p5, p6, poradie, nazev FROM food600.sync_idriadok WHERE operacia=''I'' AND is_master>0 AND idr>'||(midremoteinsert::CHARACTER(10))||' AND id_sync<>'||(mojeid::CHARACTER(10))||' ORDER BY idr') as remzmeny(idr bigint, prikaz character varying, idriadok integer, tabulka character varying, p1 character varying, p2 character varying, p3 character varying, p4 character varying, p5 character varying, p6 character varying, poradie INTEGER, nazev character(30))) aaa ORDER BY poradie DESC, idr;
zoznamremupdatovostatne CURSOR FOR SELECT idr, prikaz, idriadok, tabulka, p1, p2, p3, p4, p5, p6, poradie, nazev FROM (SELECT * FROM dblink('connstrx','SELECT idr, prikaz, idriadok, tabulka, p1, p2, p3, p4, p5, p6, poradie, nazev FROM food600.sync_idriadok WHERE operacia=''U'' AND is_master>0 AND idr>'||(midremoteupdate::CHARACTER(10))||' AND id_sync<>'||(mojeid::CHARACTER(10))||' ORDER BY idr') as remzmeny(idr bigint, prikaz character varying, idriadok integer, tabulka character varying, p1 character varying, p2 character varying, p3 character varying, p4 character varying, p5 character varying, p6 character varying, poradie INTEGER, nazev character(30))) aaa ORDER BY poradie DESC, idr;
zoznamdeletovostatne CURSOR FOR SELECT idr, prikaz, idriadok, tabulka, p1, p2, p3, p4, p5, p6, poradie, nazev FROM food600.sync_idriadok WHERE operacia='D' AND idr>midlocaldelete AND is_master=0 ORDER BY poradie DESC, idr;
zoznamremdeletovostatne CURSOR FOR SELECT idr, prikaz, idriadok, tabulka, p1, p2, p3, p4, p5, p6, poradie, nazev FROM (SELECT * FROM dblink('connstrx','SELECT idr, prikaz, idriadok, tabulka, p1, p2, p3, p4, p5, p6, poradie, nazev FROM food600.sync_idriadok WHERE operacia=''D'' AND is_master>0 AND idr>'||(midremotedelete::CHARACTER(10))||' ORDER BY idr') as remzmeny(idr bigint, prikaz character varying, idriadok integer, tabulka character varying, p1 character varying, p2 character varying, p3 character varying, p4 character varying, p5 character varying, p6 character varying, poradie INTEGER, nazev character(30))) aaa ORDER BY poradie DESC, idr;

---- L.L. (10.12.2021)  Samostana tabulka hkredobr, pretoze 1. hkredobr uz obsahoval id_sync, ktore sa inak pouzivalo a 2. nestacilo %M1-M6%
zoznaminsertovhkredobr CURSOR FOR SELECT idr, prikaz, idriadok, tabulka, p1, p2, p3, p4, p5, p6, p7, p8, poradie, nazev  FROM food600.sync_hkredobr WHERE operacia='I' AND idr>midlocalinserthkredobr AND is_master=0 ORDER BY poradie DESC, idr;
zoznamupdatovhkredobr CURSOR FOR SELECT idr, prikaz, idriadok, tabulka, p1, p2, p3, p4, p5, p6, p7, p8, poradie, nazev FROM food600.sync_hkredobr WHERE operacia='U' AND idr>midlocalupdatehkredobr AND is_master=0 ORDER BY poradie DESC, idr;
zoznamimportovhkredobr CURSOR FOR SELECT idr, prikaz, idriadok, tabulka, p1, p2, p3, p4, p5, p6, p7, p8, poradie, nazev FROM (SELECT * FROM dblink('connstrx','SELECT idr, prikaz, idriadok, tabulka, p1, p2, p3, p4, p5, p6, p7, p8, poradie, nazev FROM food600.sync_hkredobr WHERE operacia=''I'' AND is_master>0 AND idr>'||(midremoteinserthkredobr::CHARACTER(10))||' AND id_syncf<>'||(mojeid::CHARACTER(10))||' ORDER BY idr') as remzmeny(idr bigint, prikaz character varying, idriadok integer, tabulka character varying, p1 character varying, p2 character varying, p3 character varying, p4 character varying, p5 character varying, p6 character varying, p7 character varying, p8 character varying, poradie INTEGER, nazev character(30))) aaa ORDER BY poradie DESC, idr;
zoznamremupdatovhkredobr CURSOR FOR SELECT idr, prikaz, idriadok, tabulka, p1, p2, p3, p4, p5, p6, p7, p8, poradie, nazev FROM (SELECT * FROM dblink('connstrx','SELECT idr, prikaz, idriadok, tabulka, p1, p2, p3, p4, p5, p6, p7, p8, poradie, nazev FROM food600.sync_hkredobr WHERE operacia=''U'' AND is_master>0 AND idr>'||(midremoteupdatehkredobr::CHARACTER(10))||' AND id_syncf<>'||(mojeid::CHARACTER(10))||' ORDER BY idr') as remzmeny(idr bigint, prikaz character varying, idriadok integer, tabulka character varying, p1 character varying, p2 character varying, p3 character varying, p4 character varying, p5 character varying, p6 character varying, p7 character varying, p8 character varying, poradie INTEGER, nazev character(30))) aaa ORDER BY poradie DESC, idr;
zoznamdeletovhkredobr CURSOR FOR SELECT idr, prikaz, idriadok, tabulka, p1, p2, p3, p4, p5, p6, p7, p8, poradie, nazev FROM food600.sync_hkredobr WHERE operacia='D' AND idr>midlocaldeletehkredobr AND is_master=0 ORDER BY poradie DESC, idr;
zoznamremdeletovhkredobr CURSOR FOR SELECT idr, prikaz, idriadok, tabulka, p1, p2, p3, p4, p5, p6, p7, p8, poradie, nazev FROM (SELECT * FROM dblink('connstrx','SELECT idr, prikaz, idriadok, tabulka, p1, p2, p3, p4, p5, p6, p7, p8, poradie, nazev FROM food600.sync_hkredobr WHERE operacia=''D'' AND is_master>0 AND idr>'||(midremotedeletehkredobr::CHARACTER(10))||' ORDER BY idr') as remzmeny(idr bigint, prikaz character varying, idriadok integer, tabulka character varying, p1 character varying, p2 character varying, p3 character varying, p4 character varying, p5 character varying, p6 character varying, p7 character varying, p8 character varying, poradie INTEGER, nazev character(30))) aaa ORDER BY poradie DESC, idr;


BEGIN
SELECT idriadok, id_master INTO mojeid, mojmaster FROM food600.sync_set WHERE tosomja>0 LIMIT 1;
IF zobrazuj THEN
	RAISE NOTICE 'mojeid:%',mojeid;
	RAISE NOTICE 'mojmaster:%',mojmaster;
END IF;
IF mojeid IS NULL THEN
	--- V nastavovacej tabulke sa nenachadza moj zaznam, synchronizacia sa nerobi
	mojeid:=0;
	mojmaster:=0;
END IF;       
IF mojeid>0 AND mojmaster>0 THEN
	---- L.L. (10.12.2021)  Samostana tabulka hkredobr, pretoze 1. hkredobr uz obsahoval id_sync, ktore sa inak pouzivalo a 2. nestacilo %M1-M6%
	SELECT ip, port, databaza, meno, heslo, id_local_delete, id_remote_delete, id_local_insert, id_local_update, id_remote_insert, id_remote_update, id_local_insert_karty, id_local_update_karty, id_remote_insert_karty, id_remote_update_karty, id_local_insert_skupiny, id_local_update_skupiny, id_remote_insert_skupiny, id_remote_update_skupiny, id_local_insert_hkredobr, id_local_update_hkredobr, id_local_delete_hkredobr, id_remote_insert_hkredobr, id_remote_update_hkredobr, id_remote_delete_hkredobr  
		INTO mip, mport, mdatabaza, mmeno, mheslo, midlocaldelete, midremotedelete, midlocalinsert, midlocalupdate, midremoteinsert, midremoteupdate, midlocalinsertkarty, midlocalupdatekarty, midremoteinsertkarty, midremoteupdatekarty, midlocalinsertskupiny, midlocalupdateskupiny, midremoteinsertskupiny, midremoteupdateskupiny, midlocalinserthkredobr, midlocalupdatehkredobr, midlocaldeletehkredobr, midremoteinserthkredobr, midremoteupdatehkredobr, midremotedeletehkredobr FROM food600.sync_set WHERE idriadok=mojmaster LIMIT 1;
	SELECT ip, port, databaza, meno, heslo INTO mipja, mportja, mdatabazaja, mmenoja, mhesloja FROM food600.sync_set WHERE idriadok=mojeid LIMIT 1;
	IF (mip IS NOT NULL) AND (mport IS NOT NULL) AND (mdatabaza IS NOT NULL) AND (mmeno IS NOT NULL) AND (mheslo IS NOT NULL) THEN
		IF zobrazuj THEN 
			RAISE NOTICE 'mdatabaza:%',mdatabaza;
			RAISE NOTICE 'mip:%',mip;
			RAISE NOTICE 'mport:%',mport;
			RAISE NOTICE 'mmeno:%',mmeno;
			RAISE NOTICE 'mheslo:%',mheslo;
			RAISE NOTICE 'midlocaldelete:%',midlocaldelete;
			RAISE NOTICE 'midremotedelete:%',midremotedelete;
			RAISE NOTICE 'midlocalinsert:%',midlocalinsert;
			RAISE NOTICE 'midremoteinsert:%',midremoteinsert;
			RAISE NOTICE 'midlocalupdate:%',midlocalupdate;
			RAISE NOTICE 'midremoteupdate:%',midremoteupdate;
			RAISE NOTICE 'midlocalinsertkarty:%',midlocalinsertkarty;
			RAISE NOTICE 'midremoteinsertkarty:%',midremoteinsertkarty;
			RAISE NOTICE 'midlocalupdatekarty:%',midlocalupdatekarty;
			RAISE NOTICE 'midremoteupdatekarty:%',midremoteupdatekarty;
			RAISE NOTICE 'midlocalinsertskupiny:%',midlocalinsertskupiny;
			RAISE NOTICE 'midremoteinsertskupiny:%',midremoteinsertskupiny;
			RAISE NOTICE 'midlocalupdateskupiny:%',midlocalupdateskupiny;
			RAISE NOTICE 'midremoteupdateskupiny:%',midremoteupdateskupiny;
			---- L.L. (10.12.2021)  Samostana tabulka hkredobr, pretoze 1. hkredobr uz obsahoval id_sync, ktore sa inak pouzivalo a 2. nestacilo %M1-M6%
			RAISE NOTICE 'midlocaldeletehkredobr:%',midlocaldeletehkredobr;
			RAISE NOTICE 'midremotedeletehkredobr:%',midremotedeletehkredobr;
			RAISE NOTICE 'midlocalinserthkredobr:%',midlocalinserthkredobr;
			RAISE NOTICE 'midremoteinserthkredobr:%',midremoteinserthkredobr;
			RAISE NOTICE 'midlocalupdatehkredobr:%',midlocalupdatehkredobr;
			RAISE NOTICE 'midremoteupdatehkredobr:%',midremoteupdatehkredobr;

		END IF;
		BEGIN
			RAISE NOTICE 'hostaddr=%; port=%; dbname=%; user=%; password=%',btrim(mip),btrim(mport),btrim(mdatabaza),btrim(mmeno),btrim(mheslo);
			PERFORM dblink_connect('connstrx','hostaddr='||btrim(mip)||' port='||btrim(mport)||' dbname='||btrim(mdatabaza)||' user='||btrim(mmeno)||' password='||btrim(mheslo));
		EXCEPTION
			WHEN duplicate_object THEN --code error 42710
				RAISE NOTICE 'this connections exists';
				RAISE NOTICE '%',sqlerrm;
			WHEN others	THEN
				RAISE NOTICE 'nepripojene:%', sqlstate;
				RAISE NOTICE '%',sqlerrm;
				pokracuj:=0;
		END;
		IF pokracuj>0 THEN
			mdone := 100000;
			OPEN zoznaminsertovskupiny;
			IF zobrazuj THEN
				RAISE NOTICE 'otvoreny zoznam insertov skupin';
			END IF;
			WHILE (mdone>0) AND (pokracuj>0) LOOP
				FETCH zoznaminsertovskupiny INTO midriadok, mprikaz, mcdruh, mdruh, mp1, mp2, mp3, mp4, mp5, mp6;
				IF midriadok ISNULL THEN
					mdone:=0;
				ELSE
					IF zobrazuj THEN 
						RAISE NOTICE 'idriadok:%', midriadok;
						RAISE NOTICE 'mprikaz:%', mprikaz;
					END IF;
					IF mp6 IS NOT NULL AND btrim(mp6)<>'' THEN
						EXECUTE('SELECT food600.'||btrim(mp6)) INTO nahrada;
						mprikaz:=REPLACE(mprikaz,'%P6%',nahrada);
					END IF;
					IF mp5 IS NOT NULL AND btrim(mp5)<>'' THEN
						EXECUTE('SELECT food600.'||btrim(mp5)) INTO nahrada;
						mprikaz:=REPLACE(mprikaz,'%P5%',nahrada);
					END IF;
					IF mp4 IS NOT NULL AND btrim(mp4)<>'' THEN
						EXECUTE('SELECT food600.'||btrim(mp4)) INTO nahrada;
						mprikaz:=REPLACE(mprikaz,'%P4%',nahrada);
					END IF;
					IF mp3 IS NOT NULL AND btrim(mp3)<>'' THEN
						EXECUTE('SELECT food600.'||btrim(mp3)) INTO nahrada;
						mprikaz:=REPLACE(mprikaz,'%P3%',nahrada);
					END IF;
					IF mp2 IS NOT NULL AND btrim(mp2)<>'' THEN
						EXECUTE('SELECT food600.'||btrim(mp2)) INTO nahrada;
						mprikaz:=REPLACE(mprikaz,'%P2%',nahrada);
					END IF;
					IF mp1 IS NOT NULL AND btrim(mp1)<>'' THEN
						EXECUTE('SELECT food600.'||btrim(mp1)) INTO nahrada;
						mprikaz:=REPLACE(mprikaz,'%P1%',nahrada);
					END IF;
					BEGIN
						SELECT c_druh INTO mcdruhmaster FROM (SELECT * FROM dblink('connstrx',mprikaz) as remdruh(c_druh integer)) aaa ;
					EXCEPTION
						WHEN unique_violation THEN
							RAISE NOTICE 'duplicitny druh';
							SELECT c_druh INTO mcdruhmaster FROM (SELECT * FROM dblink('connstrx','SELECT c_druh FROM food600.skupiny WHERE druh='''||mdruh||'''') as remdruh(c_druh integer)) aaa ;
							IF mcdruhmaster IS NULL THEN
								mcdruhmaster:=mcdruh;
							END IF;
						WHEN others	THEN
							RAISE NOTICE 'chyba:%', sqlstate;
							RAISE NOTICE '%',sqlerrm;
							mcdruhmaster:=null;
							pokracuj:=0;
					END;
					IF zobrazuj THEN 
						RAISE NOTICE 'mcdruhmaster2:%', mcdruhmaster;
					END IF;
					IF mcdruhmaster IS NOT NULL THEN
						UPDATE food600.skupiny SET c_d_m=mcdruhmaster WHERE c_druh=mcdruh;
						UPDATE food600.sync_set SET id_local_insert_skupiny=midriadok WHERE idriadok=mojmaster;
					END IF;
				END IF;
			END LOOP;		
			CLOSE zoznaminsertovskupiny;	
			IF pokracuj>0 THEN
				mdone := 100000;
				OPEN zoznamimportovskupiny;
				IF zobrazuj THEN 
					RAISE NOTICE 'otvoreny zoznam importov skupin';
				END IF;
				WHILE (mdone>0) AND (pokracuj>0) LOOP
					FETCH zoznamimportovskupiny INTO midriadok, mprikaz, mcdruh, mdruh, mp1, mp2, mp3, mp4, mp5, mp6;
					IF midriadok ISNULL THEN
						mdone:=0;
					ELSE
						IF zobrazuj THEN
							RAISE NOTICE 'idriadok:%', midriadok;
							RAISE NOTICE 'mprikaz:%', mprikaz;
						END IF;
						IF mp6 IS NOT NULL AND btrim(mp6)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp6)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P6%',nahrada);
						END IF;
						IF mp5 IS NOT NULL AND btrim(mp5)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp5)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P5%',nahrada);
						END IF;
						IF mp4 IS NOT NULL AND btrim(mp4)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp4)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P4%',nahrada);
						END IF;
						IF mp3 IS NOT NULL AND btrim(mp3)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp3)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P3%',nahrada);
						END IF;
						IF mp2 IS NOT NULL AND btrim(mp2)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp2)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P2%',nahrada);
						END IF;
						IF mp1 IS NOT NULL AND btrim(mp1)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp1)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P1%',nahrada);
						END IF;
						BEGIN
							EXECUTE mprikaz INTO mcdruhmaster;
						EXCEPTION
							WHEN unique_violation THEN
								RAISE NOTICE 'duplicitna skupina pri importe';
 								WITH updated AS (UPDATE food600.skupiny SET c_d_m=mcdruh WHERE druh=mdruh AND (c_d_m=0 OR c_d_m IS NULL) RETURNING c_druh) 
								SELECT c_druh INTO mcdruhmaster FROM updated LIMIT 1;
								IF mcdruhmaster IS NULL THEN
									RAISE NOTICE 'duplicitna skupina pri importe uz bola sparovana s inou master skupinou';
									--- teoreticky tu moze vzniknut problem, ak ostane skupina s rovnakym menom, ale inou priradenou master skupinou
								END IF;
							WHEN others	THEN
								RAISE NOTICE 'chyba:%', sqlstate;
								RAISE NOTICE '%',sqlerrm;
								mcdruhmaster:=null;
								pokracuj:=0;
						END;
						IF zobrazuj THEN
							RAISE NOTICE 'mcdruhmaster2:%', mcdruhmaster;
						END IF;
						IF mcdruhmaster IS NOT NULL THEN
							UPDATE food600.skupiny SET c_d_m=mcdruh WHERE c_druh=mcdruhmaster;
							UPDATE food600.sync_skupiny SET prikaz=REPLACE(prikaz,'%P1%',btrim(mcdruhmaster::character(20))), p1='' WHERE operacia='D'  AND c_druh=mcdruh;
							UPDATE food600.sync_set SET id_remote_insert_skupiny=midriadok WHERE idriadok=mojmaster;
						END IF;
					END IF;
				END LOOP;		
				CLOSE zoznamimportovskupiny;	
			END IF;
			IF pokracuj>0 THEN
				mdone := 100000;
				OPEN zoznamupdatovskupiny;
				IF zobrazuj THEN
					RAISE NOTICE 'otvoreny zoznam updatov skupin';
				END IF;
				WHILE (mdone>0) AND (pokracuj>0) LOOP
					FETCH zoznamupdatovskupiny INTO midriadok, mprikaz, mckarty, mnazev, mp1, mp2, mp3, mp4, mp5, mp6;
					IF midriadok ISNULL THEN
						mdone:=0;
					ELSE
						IF zobrazuj THEN
							RAISE NOTICE 'idriadok:%', midriadok;
							RAISE NOTICE 'mprikaz:%', mprikaz;
						END IF;
						IF mp6 IS NOT NULL AND btrim(mp6)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp6)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P6%',nahrada);
						END IF;
						IF mp5 IS NOT NULL AND btrim(mp5)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp5)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P5%',nahrada);
						END IF;
						IF mp4 IS NOT NULL AND btrim(mp4)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp4)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P4%',nahrada);
						END IF;
						IF mp3 IS NOT NULL AND btrim(mp3)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp3)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P3%',nahrada);
						END IF;
						IF mp2 IS NOT NULL AND btrim(mp2)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp2)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P2%',nahrada);
						END IF;
						IF mp1 IS NOT NULL AND btrim(mp1)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp1)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P1%',nahrada);
						END IF;
						BEGIN
							PERFORM dblink_exec('connstrx',mprikaz);
						EXCEPTION
							WHEN unique_violation THEN
								RAISE NOTICE 'premenovanim vznikla duplicitna skupina';
								---- Skusim najst master skupinu s rovnakym menom a prepojim ich
								SELECT c_druh INTO mcdruhmaster FROM (SELECT * FROM dblink('connstrx','SELECT c_druh FROM food600.skupiny WHERE druh='''||mdruh||'''') as remdruh(c_druh integer)) aaa ;
								IF mcdruhmaster IS NULL THEN
									--- master skupina s rovnakym menom sa nenasla - predpokladam, ze spadla linka
									mcdruhmaster:=null;
									pokracuj:=0;
								ELSE
									--- nasla sa master skupina s rovnakym menom, prepojim ich
									UPDATE food600.skupiny SET c_d_m=mcdruhmaster WHERE c_druh=mcdruh;
								END IF;
							WHEN others	THEN
								RAISE NOTICE 'chyba:%', sqlstate;
								RAISE NOTICE '%',sqlerrm;
								mcdruhmaster:=null;
								pokracuj:=0;
						END;
						IF pokracuj>0 THEN
							UPDATE food600.sync_set SET id_local_update_skupiny=midriadok WHERE idriadok=mojmaster;
						END IF;
					END IF;
				END LOOP;		
				CLOSE zoznamupdatovskupiny;	
			END IF;
			IF pokracuj>0 THEN
				mdone := 100000;
				OPEN zoznamremupdatovskupiny;
				IF zobrazuj THEN
					RAISE NOTICE 'otvoreny zoznam remupdatov skupin';
				END IF;
				WHILE (mdone>0) AND (pokracuj>0) LOOP
					FETCH zoznamremupdatovskupiny INTO midriadok, mprikaz, mcdruh, mdruh, mp1, mp2, mp3, mp4, mp5, mp6;
					IF midriadok ISNULL THEN
						mdone:=0;
					ELSE
						IF zobrazuj THEN
							RAISE NOTICE 'idriadok:%', midriadok;
							RAISE NOTICE 'mprikaz:%', mprikaz;
						END IF;
						IF mp6 IS NOT NULL AND btrim(mp6)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp6)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P6%',nahrada);
						END IF;
						IF mp5 IS NOT NULL AND btrim(mp5)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp5)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P5%',nahrada);
						END IF;
						IF mp4 IS NOT NULL AND btrim(mp4)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp4)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P4%',nahrada);
						END IF;
						IF mp3 IS NOT NULL AND btrim(mp3)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp3)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P3%',nahrada);
						END IF;
						IF mp2 IS NOT NULL AND btrim(mp2)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp2)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P2%',nahrada);
						END IF;
						IF mp1 IS NOT NULL AND btrim(mp1)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp1)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P1%',nahrada);
						END IF;
						BEGIN
							EXECUTE mprikaz;
						EXCEPTION
							WHEN unique_violation THEN
								RAISE NOTICE 'premenovanim vznikla duplicitna skupina';
								---- Skusim najst master skupinu s rovnakym menom a prepojim ich
								SELECT c_druh INTO mcdruhmaster FROM food600.skupiny WHERE druh=mdruh;
								IF mcdruhmaster IS NULL THEN
									--- lokalna skupina s rovnakym menom sa nenasla - neviem preco
									mcdruhmaster:=null;
									pokracuj:=0;
								ELSE
									--- nasla sa lokalna skupina s rovnakym menom, prepojim ich
									UPDATE food600.skupiny SET c_d_m=mcdruh WHERE c_druh=mcdruhmaster;
								END IF;
							WHEN others	THEN
								RAISE NOTICE 'chyba:%', sqlstate;
								RAISE NOTICE '%',sqlerrm;
								mcdruhmaster:=null;
								pokracuj:=0;
						END;
						IF pokracuj>0 THEN
							UPDATE food600.sync_set SET id_remote_update_skupiny=midriadok WHERE idriadok=mojmaster;
						END IF;
					END IF;
				END LOOP;		
				CLOSE zoznamremupdatovskupiny;	
			END IF;

			mdone := 100000;
			OPEN zoznaminsertovkarty;
			IF zobrazuj THEN
				RAISE NOTICE 'otvoreny zoznam insertov karty';
			END IF;
			WHILE (mdone>0) AND (pokracuj>0) LOOP
				FETCH zoznaminsertovkarty INTO midriadok, mprikaz, mckarty, mnazev, mp1, mp2, mp3, mp4, mp5, mp6;
				IF midriadok ISNULL THEN
					mdone:=0;
				ELSE
					IF zobrazuj THEN
						RAISE NOTICE 'idriadok:%', midriadok;
						RAISE NOTICE 'mprikaz:%', mprikaz;
					END IF;
					IF mp6 IS NOT NULL AND btrim(mp6)<>'' THEN
						EXECUTE('SELECT food600.'||btrim(mp6)) INTO nahrada;
						mprikaz:=REPLACE(mprikaz,'%P6%',nahrada);
					END IF;
					IF mp5 IS NOT NULL AND btrim(mp5)<>'' THEN
						EXECUTE('SELECT food600.'||btrim(mp5)) INTO nahrada;
						mprikaz:=REPLACE(mprikaz,'%P5%',nahrada);
					END IF;
					IF mp4 IS NOT NULL AND btrim(mp4)<>'' THEN
						EXECUTE('SELECT food600.'||btrim(mp4)) INTO nahrada;
						mprikaz:=REPLACE(mprikaz,'%P4%',nahrada);
					END IF;
					IF mp3 IS NOT NULL AND btrim(mp3)<>'' THEN
						EXECUTE('SELECT food600.'||btrim(mp3)) INTO nahrada;
						mprikaz:=REPLACE(mprikaz,'%P3%',nahrada);
					END IF;
					IF mp2 IS NOT NULL AND btrim(mp2)<>'' THEN
						EXECUTE('SELECT food600.'||btrim(mp2)) INTO nahrada;
						mprikaz:=REPLACE(mprikaz,'%P2%',nahrada);
					END IF;
					IF mp1 IS NOT NULL AND btrim(mp1)<>'' THEN
						EXECUTE('SELECT food600.'||btrim(mp1)) INTO nahrada;
						mprikaz:=REPLACE(mprikaz,'%P1%',nahrada);
					END IF;
					IF zobrazuj THEN
						RAISE NOTICE 'mprikaz po uprave:%', mprikaz;
					END IF;
					BEGIN
						SELECT c_karty INTO mckartymaster FROM (SELECT * FROM dblink('connstrx',mprikaz) as remkarta(c_karty integer)) aaa ;
					EXCEPTION
						WHEN unique_violation THEN
							RAISE NOTICE 'duplicitna karta';
							SELECT c_karty INTO mckartymaster FROM (SELECT * FROM dblink('connstrx','SELECT c_karty FROM food600.karty WHERE nazev='''||mnazev||'''') as remkarta(c_karty integer)) aaa ;
							IF mckartymaster IS NULL THEN
								mckartymaster:=mckarty;
							END IF;
						WHEN others	THEN
							RAISE NOTICE 'chyba:%', sqlstate;
							RAISE NOTICE '%',sqlerrm;
							mckartymaster:=null;
							pokracuj:=0;
					END;
					IF zobrazuj THEN
						RAISE NOTICE 'mckartymaster2:%', mckartymaster;
					END IF;
					IF mckartymaster IS NOT NULL THEN
						UPDATE food600.karty SET c_k_m=mckartymaster WHERE c_karty=mckarty;
						UPDATE food600.sync_set SET id_local_insert_karty=midriadok WHERE idriadok=mojmaster;
					END IF;
				END IF;
			END LOOP;		
			CLOSE zoznaminsertovkarty;	
			IF pokracuj>0 THEN
				mdone := 100000;
				OPEN zoznamimportovkarty;
				IF zobrazuj THEN
					RAISE NOTICE 'otvoreny zoznam importov karty';
				END IF;
				WHILE (mdone>0) AND (pokracuj>0) LOOP
					FETCH zoznamimportovkarty INTO midriadok, mprikaz, mckarty, mnazev, mp1, mp2, mp3, mp4, mp5, mp6;
					IF midriadok ISNULL THEN
						mdone:=0;
					ELSE
						IF zobrazuj THEN
							RAISE NOTICE 'idriadok:%', midriadok;
							RAISE NOTICE 'mprikaz:%', mprikaz;
						END IF;
						IF mp6 IS NOT NULL AND btrim(mp6)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp6)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P6%',nahrada);
						END IF;
						IF mp5 IS NOT NULL AND btrim(mp5)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp5)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P5%',nahrada);
						END IF;
						IF mp4 IS NOT NULL AND btrim(mp4)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp4)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P4%',nahrada);
						END IF;
						IF mp3 IS NOT NULL AND btrim(mp3)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp3)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P3%',nahrada);
						END IF;
						IF mp2 IS NOT NULL AND btrim(mp2)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp2)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P2%',nahrada);
						END IF;
						IF mp1 IS NOT NULL AND btrim(mp1)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp1)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P1%',nahrada);
						END IF;
						IF zobrazuj THEN
							RAISE NOTICE 'mprikaz:%', mprikaz;
						END IF;
						BEGIN
							EXECUTE mprikaz INTO mckartymaster;
						EXCEPTION
							WHEN unique_violation THEN
								RAISE NOTICE 'duplicitna karta pri importe';
 								WITH updated AS (UPDATE food600.karty SET c_k_m=mckarty WHERE nazev=mnazev AND (c_k_m=0 OR c_k_m IS NULL) RETURNING c_karty) 
								SELECT c_karty INTO mckartymaster FROM updated LIMIT 1;
								IF mckartymaster IS NULL THEN
									RAISE NOTICE 'duplicitna karta pri importe uz bola sparovana s inou master kartou';
									--- teoreticky tu moze vzniknut problem, ak ostane karta s rovnakym menom, ale inou priradenou master kartou
								END IF;
							WHEN others	THEN
								RAISE NOTICE 'chyba:%', sqlstate;
								RAISE NOTICE '%',sqlerrm;
								mckartymaster:=null;
								pokracuj:=0;
						END;
						IF zobrazuj THEN
							RAISE NOTICE 'mckartymaster2:%', mckartymaster;
						END IF;
						IF mckartymaster IS NOT NULL THEN
							UPDATE food600.karty SET c_k_m=mckarty WHERE c_karty=mckartymaster;
							UPDATE food600.sync_set SET id_remote_insert_karty=midriadok WHERE idriadok=mojmaster;
						END IF;
					END IF;
				END LOOP;		
				CLOSE zoznamimportovkarty;	
			END IF;
			IF pokracuj>0 THEN
				mdone := 100000;
				OPEN zoznamupdatovkarty;
				IF zobrazuj THEN
					RAISE NOTICE 'otvoreny zoznam updatov karty';
				END IF;
				WHILE (mdone>0) AND (pokracuj>0) LOOP
					FETCH zoznamupdatovkarty INTO midriadok, mprikaz, mckarty, mnazev, mp1, mp2, mp3, mp4, mp5, mp6;
					IF midriadok ISNULL THEN
						mdone:=0;
					ELSE
						IF zobrazuj THEN
							RAISE NOTICE 'idriadok:%', midriadok;
							RAISE NOTICE 'mprikaz:%', mprikaz;
						END IF;
						IF mp6 IS NOT NULL AND btrim(mp6)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp6)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P6%',nahrada);
						END IF;
						IF mp5 IS NOT NULL AND btrim(mp5)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp5)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P5%',nahrada);
						END IF;
						IF mp4 IS NOT NULL AND btrim(mp4)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp4)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P4%',nahrada);
						END IF;
						IF mp3 IS NOT NULL AND btrim(mp3)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp3)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P3%',nahrada);
						END IF;
						IF mp2 IS NOT NULL AND btrim(mp2)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp2)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P2%',nahrada);
						END IF;
						IF mp1 IS NOT NULL AND btrim(mp1)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp1)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P1%',nahrada);
						END IF;
						BEGIN
							PERFORM dblink_exec('connstrx',mprikaz);
						EXCEPTION
							WHEN unique_violation THEN
								RAISE NOTICE 'premenovanim vznikla duplicitna karta';
								---- Skusim najst master kartu s rovnakym menom a prepojim ich
								SELECT c_karty INTO mckartymaster FROM (SELECT * FROM dblink('connstrx','SELECT c_karty FROM food600.karty WHERE nazev='''||mnazev||'''') as remkarta(c_karty integer)) aaa ;
								IF mckartymaster IS NULL THEN
									--- master karta s rovnakym menom sa nenasla - predpokladam, ze spadla linka
									mckartymaster:=null;
									pokracuj:=0;
								ELSE
									--- nasla sa master karta s rovnakym menom, prepojim ich
									UPDATE food600.karty SET c_k_m=mckartymaster WHERE c_karty=mckarty;
								END IF;
							WHEN others	THEN
								RAISE NOTICE 'chyba:%', sqlstate;
								RAISE NOTICE '%',sqlerrm;
								mckartymaster:=null;
								pokracuj:=0;
						END;
						IF pokracuj>0 THEN
							UPDATE food600.sync_set SET id_local_update_karty=midriadok WHERE idriadok=mojmaster;
						END IF;
					END IF;
				END LOOP;		
				CLOSE zoznamupdatovkarty;	
			END IF;
			IF pokracuj>0 THEN
				mdone := 100000;
				OPEN zoznamremupdatovkarty;
				IF zobrazuj THEN
					RAISE NOTICE 'otvoreny zoznam remupdatov karty';
				END IF;
				WHILE (mdone>0) AND (pokracuj>0) LOOP
					FETCH zoznamremupdatovkarty INTO midriadok, mprikaz, mckarty, mnazev, mp1, mp2, mp3, mp4, mp5, mp6;
					IF midriadok ISNULL THEN
						mdone:=0;
					ELSE
						IF zobrazuj THEN
							RAISE NOTICE 'idriadok:%', midriadok;
							RAISE NOTICE 'mprikaz:%', mprikaz;
						END IF;
						IF mp6 IS NOT NULL AND btrim(mp6)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp6)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P6%',nahrada);
						END IF;
						IF mp5 IS NOT NULL AND btrim(mp5)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp5)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P5%',nahrada);
						END IF;
						IF mp4 IS NOT NULL AND btrim(mp4)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp4)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P4%',nahrada);
						END IF;
						IF mp3 IS NOT NULL AND btrim(mp3)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp3)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P3%',nahrada);
						END IF;
						IF mp2 IS NOT NULL AND btrim(mp2)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp2)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P2%',nahrada);
						END IF;
						IF mp1 IS NOT NULL AND btrim(mp1)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp1)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P1%',nahrada);
						END IF;
						BEGIN
							EXECUTE mprikaz;
						EXCEPTION
							WHEN unique_violation THEN
								RAISE NOTICE 'premenovanim vznikla duplicitna karta';
								---- Skusim najst master kartu s rovnakym menom a prepojim ich
								SELECT c_karty INTO mckartymaster FROM food600.karty WHERE nazev=mnazev;
								IF mckartymaster IS NULL THEN
									--- lokalna karta s rovnakym menom sa nenasla - neviem preco
									mckartymaster:=null;
									pokracuj:=0;
								ELSE
									--- nasla sa lokalna karta s rovnakym menom, prepojim ich
									UPDATE food600.karty SET c_k_m=mckarty WHERE c_karty=mckartymaster;
								END IF;
							WHEN others	THEN
								RAISE NOTICE 'chyba:%', sqlstate;
								RAISE NOTICE '%',sqlerrm;
								mckartymaster:=null;
								pokracuj:=0;
						END;
						IF pokracuj>0 THEN
							UPDATE food600.sync_set SET id_remote_update_karty=midriadok WHERE idriadok=mojmaster;
						END IF;
					END IF;
				END LOOP;		
				CLOSE zoznamremupdatovkarty;	
			END IF;

			IF pokracuj>0 THEN
				mdone := 100000;
				OPEN zoznaminsertovostatne;
				IF zobrazuj THEN
					RAISE NOTICE 'otvoreny zoznam insertov ostatne';
				END IF;
				WHILE (mdone>0) AND (pokracuj>0) LOOP
					FETCH zoznaminsertovostatne INTO midr, mprikaz, midriadok, mtabulka, mp1, mp2, mp3, mp4, mp5, mp6, mporadie, mnazev;
					IF midr ISNULL THEN
						mdone:=0;
					ELSE
					IF zobrazuj THEN 
						RAISE NOTICE 'idr:%', midr;
						RAISE NOTICE 'mprikaz:%', mprikaz;
					END IF;
						IF mp6 IS NOT NULL AND btrim(mp6)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp6)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P6%',nahrada);
						END IF;
						IF mp5 IS NOT NULL AND btrim(mp5)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp5)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P5%',nahrada);
						END IF;
						IF mp4 IS NOT NULL AND btrim(mp4)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp4)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P4%',nahrada);
						END IF;
						IF mp3 IS NOT NULL AND btrim(mp3)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp3)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P3%',nahrada);
						END IF;
						IF mp2 IS NOT NULL AND btrim(mp2)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp2)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P2%',nahrada);
						END IF;
						IF mp1 IS NOT NULL AND btrim(mp1)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp1)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P1%',nahrada);
						END IF;
						IF btrim(mtabulka)='food_dat' THEN
							BEGIN
								SELECT idriadok INTO midmaster FROM (SELECT * FROM dblink('connstrx',mprikaz) as remkarta(idriadok CHARACTER(2))) aaa ;
							EXCEPTION
								WHEN others THEN
									RAISE NOTICE 'chyba:%', sqlstate;
									RAISE NOTICE '%',sqlerrm;
									midmaster:=null;
									pokracuj:=0;
							END;
							IF zobrazuj THEN
								RAISE NOTICE 'midmaster2:%, tabulka:%', midmaster, mtabulka;
							END IF;
							IF midmaster IS NOT NULL THEN
								IF zobrazuj THEN
									RAISE NOTICE 'midriadok:%, tabulka: %',midriadok, mtabulka;
								END IF;
								EXECUTE format('UPDATE %s SET idmaster=$1 WHERE id=$2','food600.'||btrim(mtabulka)) USING midmaster, btrim(mnazev);
								UPDATE food600.sync_set SET id_local_insert=midr WHERE idriadok=mojmaster;
							END IF;
						ELSE
							BEGIN
								SELECT idriadok INTO midriadokmaster FROM (SELECT * FROM dblink('connstrx',mprikaz) as remkarta(idriadok integer)) aaa ;
							EXCEPTION
								WHEN others THEN
									RAISE NOTICE 'chyba:%', sqlstate;
									RAISE NOTICE '%',sqlerrm;
									midriadokmaster:=null;
									pokracuj:=0;
							END;
							IF zobrazuj THEN
								RAISE NOTICE 'midriadokmaster2:%, tabulka:%', midriadokmaster, mtabulka;
							END IF;
							IF midriadokmaster IS NOT NULL THEN
								IF zobrazuj THEN
									RAISE NOTICE 'midriadok:%, tabulka: %',midriadok, mtabulka;
								END IF;
								CASE
									WHEN btrim(mtabulka)='partneri' THEN
										EXECUTE format('UPDATE %s SET c_partner_master=$1 WHERE c_partner=$2','food600.'||btrim(mtabulka)) USING midriadokmaster, midriadok;
									WHEN btrim(mtabulka)='miestnosti' THEN
										EXECUTE format('UPDATE %s SET id_miest_master=$1 WHERE id_miest=$2','food600.'||btrim(mtabulka)) USING midriadokmaster, midriadok;
									WHEN btrim(mtabulka)='spravy' THEN
										EXECUTE format('UPDATE %s SET id_sprava_master=$1 WHERE id_sprava=$2','food600.'||btrim(mtabulka)) USING midriadokmaster, midriadok;
									WHEN btrim(mtabulka)='spravyskupiny' THEN
										EXECUTE format('UPDATE %s SET id_skup_master=$1 WHERE id_skup=$2','food600.'||btrim(mtabulka)) USING midriadokmaster, midriadok;
									WHEN btrim(mtabulka)='nlimitstravnici' THEN
										EXECUTE format('UPDATE %s SET id_strav_master=$1 WHERE id_strav=$2','food600.'||btrim(mtabulka)) USING midriadokmaster, midriadok;
									WHEN btrim(mtabulka)='nlimitstatus' THEN
										EXECUTE format('UPDATE %s SET id_stat_master=$1 WHERE id_stat=$2','food600.'||btrim(mtabulka)) USING midriadokmaster, midriadok;
									WHEN btrim(mtabulka)='nlimitchody' THEN
										EXECUTE format('UPDATE %s SET id_chod_master=$1 WHERE id_chod=$2','food600.'||btrim(mtabulka)) USING midriadokmaster, midriadok;
									WHEN btrim(mtabulka)='nlimittypy' THEN
										EXECUTE format('UPDATE %s SET id_typ_master=$1 WHERE id_typ=$2','food600.'||btrim(mtabulka)) USING midriadokmaster, midriadok;
									WHEN btrim(mtabulka)='nlimit_hl' THEN
										EXECUTE format('UPDATE %s SET id_limit_master=$1 WHERE id_limit=$2','food600.'||btrim(mtabulka)) USING midriadokmaster, midriadok;
									WHEN btrim(mtabulka)='nlimitdni' THEN
										EXECUTE format('UPDATE %s SET id_den_master=$1 WHERE id_den=$2','food600.'||btrim(mtabulka)) USING midriadokmaster, midriadok;
									WHEN btrim(mtabulka)='nlimitrov' THEN
										EXECUTE format('UPDATE %s SET id_rov_master=$1 WHERE id_rov=$2','food600.'||btrim(mtabulka)) USING midriadokmaster, midriadok;
									WHEN btrim(mtabulka)='nlimitsablonyhl' THEN
										EXECUTE format('UPDATE %s SET id_limit_master=$1 WHERE id_limit=$2','food600.'||btrim(mtabulka)) USING midriadokmaster, midriadok;
									WHEN btrim(mtabulka)='nlimitsablonydni' THEN
										EXECUTE format('UPDATE %s SET id_den_master=$1 WHERE id_den=$2','food600.'||btrim(mtabulka)) USING midriadokmaster, midriadok;
									WHEN btrim(mtabulka)='hk_operacie' THEN	
										EXECUTE format('UPDATE %s SET id_oper_master=$1 WHERE id_oper=$2','kredit.'||btrim(mtabulka)) USING midriadokmaster, midriadok;
									WHEN btrim(mtabulka)='orgstr' THEN	
										EXECUTE format('UPDATE %s SET cislo_org_master=$1 WHERE cislo_org=$2','kredit.'||btrim(mtabulka)) USING midriadokmaster, midriadok;
									WHEN btrim(mtabulka)='hk_typk' THEN	
										EXECUTE format('UPDATE %s SET idriadok_master=$1 WHERE idriadok=$2','kredit.'||btrim(mtabulka)) USING midriadokmaster, midriadok;
									WHEN btrim(mtabulka)='hk_typk_operacie' THEN	
										EXECUTE format('UPDATE %s SET idriadok_master=$1 WHERE idriadok=$2','kredit.'||btrim(mtabulka)) USING midriadokmaster, midriadok;
									WHEN btrim(mtabulka)='hot_kred' THEN	
										EXECUTE format('UPDATE %s SET id_ucet_master=$1 WHERE id_ucet=$2','kredit.'||btrim(mtabulka)) USING midriadokmaster, midriadok;
									WHEN btrim(mtabulka)='hk_oskar' THEN	
										EXECUTE format('UPDATE %s SET id_karty_master=$1 WHERE id_karty=$2','kredit.'||btrim(mtabulka)) USING midriadokmaster, midriadok;
									WHEN btrim(mtabulka)='hk_ptypk' THEN	
										EXECUTE format('UPDATE %s SET idriadok_master=$1 WHERE idriadok=$2','kredit.'||btrim(mtabulka)) USING midriadokmaster, midriadok;
									WHEN btrim(mtabulka)='hk_ptypk_druh_pl' THEN	
										EXECUTE format('UPDATE %s SET idriadok_master=$1 WHERE idriadok=$2','kredit.'||btrim(mtabulka)) USING midriadokmaster, midriadok;
									WHEN btrim(mtabulka)='hk_ptypk_zakaz_drpl' THEN	
										EXECUTE format('UPDATE %s SET idriadok_master=$1 WHERE idriadok=$2','kredit.'||btrim(mtabulka)) USING midriadokmaster, midriadok;
									WHEN btrim(mtabulka)='hk_ptypk_zakaz_oper' THEN	
										EXECUTE format('UPDATE %s SET idriadok_master=$1 WHERE idriadok=$2','kredit.'||btrim(mtabulka)) USING midriadokmaster, midriadok;
									WHEN btrim(mtabulka)='hk_ptypk_spart' THEN	
										EXECUTE format('UPDATE %s SET idriadok_master=$1 WHERE idriadok=$2','kredit.'||btrim(mtabulka)) USING midriadokmaster, midriadok;
									WHEN btrim(mtabulka)='hk_slevy' THEN	
										EXECUTE format('UPDATE %s SET idriadok_master=$1 WHERE idriadok=$2','kredit.'||btrim(mtabulka)) USING midriadokmaster, midriadok;
									WHEN btrim(mtabulka)='hk_sj_hlav' THEN	
										EXECUTE format('UPDATE %s SET idriadok_master=$1 WHERE idriadok=$2','kredit.'||btrim(mtabulka)) USING midriadokmaster, midriadok;
									ELSE
										EXECUTE format('UPDATE %s SET idriadok_master=$1 WHERE idriadok=$2','food600.'||btrim(mtabulka)) USING midriadokmaster, midriadok;
								END CASE;
								UPDATE food600.sync_idriadok SET prikaz=REPLACE(prikaz,'%P1%',btrim(midriadokmaster::character(20))), p1='' WHERE operacia='D' AND tabulka=btrim(mtabulka) AND idriadok=midriadok;
								UPDATE food600.sync_set SET id_local_insert=midr WHERE idriadok=mojmaster;
							END IF;
						END IF;
					END IF;
				END LOOP;		
				CLOSE zoznaminsertovostatne;	
				IF pokracuj>0 THEN
					mdone := 100000;
					OPEN zoznamimportovostatne;
					IF zobrazuj THEN
						RAISE NOTICE 'otvoreny zoznam importov ostatnych tabuliek';
					END IF;
					WHILE (mdone>0) AND (pokracuj>0) LOOP
						FETCH zoznamimportovostatne INTO midr, mprikaz, midriadok, mtabulka, mp1, mp2, mp3, mp4, mp5, mp6, mporadie, mnazev;
						IF midr ISNULL THEN
							mdone:=0;
						ELSE
							IF zobrazuj THEN
								RAISE NOTICE 'idr:%', midr;
								RAISE NOTICE 'mprikaz:%', mprikaz;
							END IF;
							IF mp6 IS NOT NULL AND btrim(mp6)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp6)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P6%',nahrada);
							END IF;
							IF mp5 IS NOT NULL AND btrim(mp5)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp5)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P5%',nahrada);
							END IF;
							IF mp4 IS NOT NULL AND btrim(mp4)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp4)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P4%',nahrada);
							END IF;
							IF mp3 IS NOT NULL AND btrim(mp3)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp3)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P3%',nahrada);
							END IF;
							IF mp2 IS NOT NULL AND btrim(mp2)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp2)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P2%',nahrada);
							END IF;
							IF mp1 IS NOT NULL AND btrim(mp1)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp1)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P1%',nahrada);
							END IF;
							IF btrim(mtabulka)='food_dat' THEN
								BEGIN
									EXECUTE mprikaz INTO midmaster;
								EXCEPTION
									WHEN others THEN
										RAISE NOTICE 'chyba:%', sqlstate;
										RAISE NOTICE '%',sqlerrm;
										midmaster:=null;
										pokracuj:=0;
								END;
								IF zobrazuj THEN
									RAISE NOTICE 'midmaster2:%, tabulka:%', midmaster, mtabulka;
								END IF;
								IF midmaster IS NOT NULL THEN
									IF zobrazuj THEN
										RAISE NOTICE 'midriadok:%, tabulka: %',midriadok, mtabulka;
									END IF;
									EXECUTE format('UPDATE %s SET idmaster=$1 WHERE id=$2','food600.'||btrim(mtabulka)) USING btrim(mnazev), midmaster;
									UPDATE food600.sync_set SET id_remote_insert=midr WHERE idriadok=mojmaster;
								END IF;
							ELSE
								BEGIN
									EXECUTE mprikaz INTO midriadokmaster;
								EXCEPTION
									WHEN others THEN
										RAISE NOTICE 'chyba:%', sqlstate;
										RAISE NOTICE '%',sqlerrm;
										midriadokmaster:=null;
										pokracuj:=0;
								END;
								IF zobrazuj THEN
									RAISE NOTICE 'midriadokmaster2:%, tabulka:%', midriadokmaster, mtabulka;
								END IF;
								IF midriadokmaster IS NOT NULL THEN
									IF zobrazuj THEN
										RAISE NOTICE 'midriadok:%, tabulka: %',midriadok, mtabulka;
									END IF;
									CASE
										WHEN btrim(mtabulka)='partneri' THEN
											EXECUTE format('UPDATE %s SET c_partner_master=$1 WHERE c_partner=$2','food600.'||btrim(mtabulka)) USING midriadok, midriadokmaster;
										WHEN btrim(mtabulka)='miestnosti' THEN
											EXECUTE format('UPDATE %s SET id_miest_master=$1 WHERE id_miest=$2','food600.'||btrim(mtabulka)) USING midriadok, midriadokmaster;
										WHEN btrim(mtabulka)='spravy' THEN
											EXECUTE format('UPDATE %s SET id_sprava_master=$1 WHERE id_sprava=$2','food600.'||btrim(mtabulka)) USING midriadok, midriadokmaster;
										WHEN btrim(mtabulka)='spravyskupiny' THEN
											EXECUTE format('UPDATE %s SET id_skup_master=$1 WHERE id_skup=$2','food600.'||btrim(mtabulka)) USING midriadok, midriadokmaster;
										WHEN btrim(mtabulka)='nlimitstravnici' THEN
											EXECUTE format('UPDATE %s SET id_strav_master=$1 WHERE id_strav=$2','food600.'||btrim(mtabulka)) USING midriadok, midriadokmaster;
										WHEN btrim(mtabulka)='nlimitstatus' THEN
											EXECUTE format('UPDATE %s SET id_stat_master=$1 WHERE id_stat=$2','food600.'||btrim(mtabulka)) USING midriadok, midriadokmaster;
										WHEN btrim(mtabulka)='nlimitchody' THEN
											EXECUTE format('UPDATE %s SET id_chod_master=$1 WHERE id_chod=$2','food600.'||btrim(mtabulka)) USING midriadok, midriadokmaster;
										WHEN btrim(mtabulka)='nlimittypy' THEN
											EXECUTE format('UPDATE %s SET id_typ_master=$1 WHERE id_typ=$2','food600.'||btrim(mtabulka)) USING midriadok, midriadokmaster;
										WHEN btrim(mtabulka)='nlimit_hl' THEN
											EXECUTE format('UPDATE %s SET id_limit_master=$1 WHERE id_limit=$2','food600.'||btrim(mtabulka)) USING midriadok, midriadokmaster;
										WHEN btrim(mtabulka)='nlimitdni' THEN
											EXECUTE format('UPDATE %s SET id_den_master=$1 WHERE id_den=$2','food600.'||btrim(mtabulka)) USING midriadok, midriadokmaster;
										WHEN btrim(mtabulka)='nlimitrov' THEN
											EXECUTE format('UPDATE %s SET id_rov_master=$1 WHERE id_rov=$2','food600.'||btrim(mtabulka)) USING midriadok, midriadokmaster;
										WHEN btrim(mtabulka)='nlimitsablonyhl' THEN
											EXECUTE format('UPDATE %s SET id_limit_master=$1 WHERE id_limit=$2','food600.'||btrim(mtabulka)) USING midriadok, midriadokmaster;
										WHEN btrim(mtabulka)='nlimitsablonydni' THEN
											EXECUTE format('UPDATE %s SET id_den_master=$1 WHERE id_den=$2','food600.'||btrim(mtabulka)) USING midriadok, midriadokmaster;
										WHEN btrim(mtabulka)='hk_operacie' THEN
											EXECUTE format('UPDATE %s SET id_oper_master=$1 WHERE id_oper=$2','kredit.'||btrim(mtabulka)) USING midriadok, midriadokmaster;											
										WHEN btrim(mtabulka)='orgstr' THEN
											EXECUTE format('UPDATE %s SET cislo_org_master=$1 WHERE cislo_org=$2','kredit.'||btrim(mtabulka)) USING midriadok, midriadokmaster;											
										WHEN btrim(mtabulka)='hot_kred' THEN
											EXECUTE format('UPDATE %s SET id_ucet_master=$1 WHERE id_ucet=$2','kredit.'||btrim(mtabulka)) USING midriadok, midriadokmaster;																					
										WHEN btrim(mtabulka)='hk_oskar' THEN
											EXECUTE format('UPDATE %s SET id_karty_master=$1 WHERE id_karty=$2','kredit.'||btrim(mtabulka)) USING midriadok, midriadokmaster;																					
										WHEN btrim(mtabulka)='hk_typk' THEN
											EXECUTE format('UPDATE %s SET idriadok_master=$1 WHERE idriadok=$2','kredit.'||btrim(mtabulka)) USING midriadok, midriadokmaster;
											UPDATE food600.sync_idriadok SET p1='', prikaz=REPLACE(prikaz,'%P1%', btrim(midriadokmaster::CHARACTER(20))) WHERE tabulka=btrim(mtabulka) AND operacia='D' AND idriadok=midriadok;
										WHEN btrim(mtabulka)='hk_typk_operacie' THEN
											EXECUTE format('UPDATE %s SET idriadok_master=$1 WHERE idriadok=$2','kredit.'||btrim(mtabulka)) USING midriadok, midriadokmaster;
											UPDATE food600.sync_idriadok SET p1='', prikaz=REPLACE(prikaz,'%P1%', btrim(midriadokmaster::CHARACTER(20))) WHERE tabulka=btrim(mtabulka) AND operacia='D' AND idriadok=midriadok;
										WHEN btrim(mtabulka)='hk_ptypk' THEN
											EXECUTE format('UPDATE %s SET idriadok_master=$1 WHERE idriadok=$2','kredit.'||btrim(mtabulka)) USING midriadok, midriadokmaster;
											UPDATE food600.sync_idriadok SET p1='', prikaz=REPLACE(prikaz,'%P1%', btrim(midriadokmaster::CHARACTER(20))) WHERE tabulka=btrim(mtabulka) AND operacia='D' AND idriadok=midriadok;
										WHEN btrim(mtabulka)='hk_ptypk_druh_pl' THEN
											EXECUTE format('UPDATE %s SET idriadok_master=$1 WHERE idriadok=$2','kredit.'||btrim(mtabulka)) USING midriadok, midriadokmaster;
											UPDATE food600.sync_idriadok SET p1='', prikaz=REPLACE(prikaz,'%P1%', btrim(midriadokmaster::CHARACTER(20))) WHERE tabulka=btrim(mtabulka) AND operacia='D' AND idriadok=midriadok;
										WHEN btrim(mtabulka)='hk_ptypk_zakaz_drpl' THEN
											EXECUTE format('UPDATE %s SET idriadok_master=$1 WHERE idriadok=$2','kredit.'||btrim(mtabulka)) USING midriadok, midriadokmaster;
											UPDATE food600.sync_idriadok SET p1='', prikaz=REPLACE(prikaz,'%P1%', btrim(midriadokmaster::CHARACTER(20))) WHERE tabulka=btrim(mtabulka) AND operacia='D' AND idriadok=midriadok;
										WHEN btrim(mtabulka)='hk_ptypk_zakaz_oper' THEN
											EXECUTE format('UPDATE %s SET idriadok_master=$1 WHERE idriadok=$2','kredit.'||btrim(mtabulka)) USING midriadok, midriadokmaster;
											UPDATE food600.sync_idriadok SET p1='', prikaz=REPLACE(prikaz,'%P1%', btrim(midriadokmaster::CHARACTER(20))) WHERE tabulka=btrim(mtabulka) AND operacia='D' AND idriadok=midriadok;
										WHEN btrim(mtabulka)='hk_ptypk_spart' THEN
											EXECUTE format('UPDATE %s SET idriadok_master=$1 WHERE idriadok=$2','kredit.'||btrim(mtabulka)) USING midriadok, midriadokmaster;
											UPDATE food600.sync_idriadok SET p1='', prikaz=REPLACE(prikaz,'%P1%', btrim(midriadokmaster::CHARACTER(20))) WHERE tabulka=btrim(mtabulka) AND operacia='D' AND idriadok=midriadok;
										WHEN btrim(mtabulka)='hk_slevy' THEN
											EXECUTE format('UPDATE %s SET idriadok_master=$1 WHERE idriadok=$2','kredit.'||btrim(mtabulka)) USING midriadok, midriadokmaster;
											UPDATE food600.sync_idriadok SET p1='', prikaz=REPLACE(prikaz,'%P1%', btrim(midriadokmaster::CHARACTER(20))) WHERE tabulka=btrim(mtabulka) AND operacia='D' AND idriadok=midriadok;
										WHEN btrim(mtabulka)='hk_sj_hlav' THEN
											EXECUTE format('UPDATE %s SET idriadok_master=$1 WHERE idriadok=$2','kredit.'||btrim(mtabulka)) USING midriadok, midriadokmaster;
											UPDATE food600.sync_idriadok SET p1='', prikaz=REPLACE(prikaz,'%P1%', btrim(midriadokmaster::CHARACTER(20))) WHERE tabulka=btrim(mtabulka) AND operacia='D' AND idriadok=midriadok;
										ELSE
											EXECUTE format('UPDATE %s SET idriadok_master=$1 WHERE idriadok=$2','food600.'||btrim(mtabulka)) USING midriadok, midriadokmaster;
											UPDATE food600.sync_idriadok SET p1='', prikaz=REPLACE(prikaz,'%P1%', btrim(midriadokmaster::CHARACTER(20))) WHERE tabulka=btrim(mtabulka) AND operacia='D' AND idriadok=midriadok;
									END CASE;
									UPDATE food600.sync_set SET id_remote_insert=midr WHERE idriadok=mojmaster;
								END IF;
							END IF;
						END IF;
					END LOOP;		
					CLOSE zoznamimportovostatne;	
				END IF;
				IF pokracuj>0 THEN
					mdone := 100000;
					OPEN zoznamupdatovostatne;
					IF zobrazuj THEN
						RAISE NOTICE 'otvoreny zoznam updatov ostatnych tabuliek';
					END IF;
					WHILE (mdone>0) AND (pokracuj>0) LOOP
						FETCH zoznamupdatovostatne INTO midr, mprikaz, midriadok, mtabulka, mp1, mp2, mp3, mp4, mp5, mp6, mporadie, mnazev;
						IF midr ISNULL THEN
							mdone:=0;
						ELSE
							IF btrim(mnazev)='bytea' THEN
								IF mp1 IS NOT NULL AND btrim(mp1)<>'' THEN
									EXECUTE('SELECT food600.'||btrim(mp1)) INTO nahrada;
								END IF;
								CASE
									WHEN btrim(mtabulka)='obaly' AND btrim(mprikaz)='obrazok' THEN
										mprikaz1:='UPDATE food600.obaly SET obrazok=remzmeny.obrazok FROM dblink(''hostaddr='||btrim(mipja)||' port='||btrim(mportja)||' dbname='||btrim(mdatabazaja)||' user='||btrim(mmenoja)||' password='||btrim(mhesloja)||''',''SELECT obrazok, '||nahrada||' as idriadok FROM food600.obaly WHERE idriadok='||btrim(midriadok::character(20))||''') AS remzmeny(obrazok BYTEA, idriadok INTEGER) WHERE obaly.idriadok=remzmeny.idriadok AND obaly.idriadok='||nahrada;
									WHEN btrim(mtabulka)='krest' AND btrim(mprikaz)='obrazok' THEN
										mprikaz1:='UPDATE food600.krest SET obrazok=remzmeny.obrazok FROM dblink(''hostaddr='||btrim(mipja)||' port='||btrim(mportja)||' dbname='||btrim(mdatabazaja)||' user='||btrim(mmenoja)||' password='||btrim(mhesloja)||''',''SELECT obrazok, '||nahrada||' as idriadok FROM food600.krest WHERE idriadok='||btrim(midriadok::character(20))||''') AS remzmeny(obrazok BYTEA, idriadok INTEGER) WHERE krest.idriadok=remzmeny.idriadok AND krest.idriadok='||nahrada;
									WHEN btrim(mtabulka)='selmist' AND btrim(mprikaz)='pozadi' THEN
										mprikaz1:='UPDATE food600.selmist SET pozadi=remzmeny.pozadi FROM dblink(''hostaddr='||btrim(mipja)||' port='||btrim(mportja)||' dbname='||btrim(mdatabazaja)||' user='||btrim(mmenoja)||' password='||btrim(mhesloja)||''',''SELECT pozadi, '||nahrada||' as idriadok FROM food600.selmist WHERE idriadok='||btrim(midriadok::character(20))||''') AS remzmeny(pozadi BYTEA, idriadok INTEGER) WHERE selmist.idriadok=remzmeny.idriadok AND selmist.idriadok='||nahrada;
								END CASE;
								BEGIN
									PERFORM dblink_exec('connstrx',mprikaz1);
								EXCEPTION
									WHEN others	THEN
										RAISE NOTICE 'chyba:%', sqlstate;
										RAISE NOTICE '%',sqlerrm;
										pokracuj:=0;
								END;
								IF pokracuj>0 THEN
									UPDATE food600.sync_set SET id_local_update=midr WHERE idriadok=mojmaster;
								END IF;
							ELSIF btrim(mnazev)='json' THEN
								IF mp1 IS NOT NULL AND btrim(mp1)<>'' THEN
									EXECUTE('SELECT food600.'||btrim(mp1)) INTO nahrada;
								END IF;
								CASE
									WHEN btrim(mtabulka)='sk__uza' AND btrim(mprikaz)='rep_uzaverka' THEN
										mprikaz1:='UPDATE food600.sk__uza SET rep_uzaverka=remzmeny.rep_uzaverka FROM dblink(''hostaddr='||btrim(mipja)||' port='||btrim(mportja)||' dbname='||btrim(mdatabazaja)||' user='||btrim(mmenoja)||' password='||btrim(mhesloja)||''',''SELECT rep_uzaverka, '||nahrada||' as idriadok FROM food600.sk__uza WHERE idriadok='||btrim(midriadok::character(20))||''') AS remzmeny(rep_uzaverka JSON, idriadok INTEGER) WHERE sk__uza.idriadok=remzmeny.idriadok AND sk__uza.idriadok='||nahrada;
									WHEN btrim(mtabulka)='sk__uza_cis' AND btrim(mprikaz)='rep_uzaverka' THEN
										mprikaz1:='UPDATE food600.sk__uza_cis SET rep_uzaverka=remzmeny.rep_uzaverka FROM dblink(''hostaddr='||btrim(mipja)||' port='||btrim(mportja)||' dbname='||btrim(mdatabazaja)||' user='||btrim(mmenoja)||' password='||btrim(mhesloja)||''',''SELECT rep_uzaverka, '||nahrada||' as idriadok FROM food600.sk__uza_cis WHERE idriadok='||btrim(midriadok::character(20))||''') AS remzmeny(rep_uzaverka JSON, idriadok INTEGER) WHERE sk__uza_cis.idriadok=remzmeny.idriadok AND sk__uza_cis.idriadok='||nahrada;
									WHEN btrim(mtabulka)='sk__txt' AND btrim(mprikaz)='info' THEN
										mprikaz1:='UPDATE food600.sk__txt SET info=remzmeny.info FROM dblink(''hostaddr='||btrim(mipja)||' port='||btrim(mportja)||' dbname='||btrim(mdatabazaja)||' user='||btrim(mmenoja)||' password='||btrim(mhesloja)||''',''SELECT info, '||nahrada||' as idriadok FROM food600.sk__txt WHERE idriadok='||btrim(midriadok::character(20))||''') AS remzmeny(info JSON, idriadok INTEGER) WHERE sk__txt.idriadok=remzmeny.idriadok AND sk__txt.idriadok='||nahrada;
									WHEN btrim(mtabulka)='sk__fin' AND btrim(mprikaz)='additional_data' THEN
										mprikaz1:='UPDATE food600.sk__fin SET info=remzmeny.additional_data FROM dblink(''hostaddr='||btrim(mipja)||' port='||btrim(mportja)||' dbname='||btrim(mdatabazaja)||' user='||btrim(mmenoja)||' password='||btrim(mhesloja)||''',''SELECT additional_data, '||nahrada||' as idriadok FROM food600.sk__fin WHERE idriadok='||btrim(midriadok::character(20))||''') AS remzmeny(additional_data JSON, idriadok INTEGER) WHERE sk__fin.idriadok=remzmeny.idriadok AND sk__fin.idriadok='||nahrada;
								END CASE;
								BEGIN
									PERFORM dblink_exec('connstrx',mprikaz1);
								EXCEPTION
									WHEN others	THEN
										RAISE NOTICE 'chyba:%', sqlstate;
										RAISE NOTICE '%',sqlerrm;
										pokracuj:=0;
								END;
								IF pokracuj>0 THEN
									UPDATE food600.sync_set SET id_local_update=midr WHERE idriadok=mojmaster;
								END IF;
							ELSIF btrim(mnazev)='jsonb' THEN
								IF mp1 IS NOT NULL AND btrim(mp1)<>'' THEN
									EXECUTE('SELECT food600.'||btrim(mp1)) INTO nahrada;
								END IF;
								CASE
									WHEN btrim(mtabulka)='sk__uza' AND btrim(mprikaz)='rep_uzaverka' THEN
										mprikaz1:='UPDATE food600.sk__uza SET rep_uzaverka=remzmeny.rep_uzaverka FROM dblink(''hostaddr='||btrim(mipja)||' port='||btrim(mportja)||' dbname='||btrim(mdatabazaja)||' user='||btrim(mmenoja)||' password='||btrim(mhesloja)||''',''SELECT rep_uzaverka, '||nahrada||' as idriadok FROM food600.sk__uza WHERE idriadok='||btrim(midriadok::character(20))||''') AS remzmeny(rep_uzaverka JSONB, idriadok INTEGER) WHERE sk__uza.idriadok=remzmeny.idriadok AND sk__uza.idriadok='||nahrada;
									WHEN btrim(mtabulka)='sk__uza_cis' AND btrim(mprikaz)='rep_uzaverka' THEN
										mprikaz1:='UPDATE food600.sk__uza_cis SET rep_uzaverka=remzmeny.rep_uzaverka FROM dblink(''hostaddr='||btrim(mipja)||' port='||btrim(mportja)||' dbname='||btrim(mdatabazaja)||' user='||btrim(mmenoja)||' password='||btrim(mhesloja)||''',''SELECT rep_uzaverka, '||nahrada||' as idriadok FROM food600.sk__uza_cis WHERE idriadok='||btrim(midriadok::character(20))||''') AS remzmeny(rep_uzaverka JSONB, idriadok INTEGER) WHERE sk__uza_cis.idriadok=remzmeny.idriadok AND sk__uza_cis.idriadok='||nahrada;
									WHEN btrim(mtabulka)='sk__txt' AND btrim(mprikaz)='info' THEN
										mprikaz1:='UPDATE food600.sk__txt SET info=remzmeny.info FROM dblink(''hostaddr='||btrim(mipja)||' port='||btrim(mportja)||' dbname='||btrim(mdatabazaja)||' user='||btrim(mmenoja)||' password='||btrim(mhesloja)||''',''SELECT info, '||nahrada||' as idriadok FROM food600.sk__txt WHERE idriadok='||btrim(midriadok::character(20))||''') AS remzmeny(info JSONB, idriadok INTEGER) WHERE sk__txt.idriadok=remzmeny.idriadok AND sk__txt.idriadok='||nahrada;
									WHEN btrim(mtabulka)='sk__fin' AND btrim(mprikaz)='additional_data' THEN
										mprikaz1:='UPDATE food600.sk__fin SET info=remzmeny.info FROM dblink(''hostaddr='||btrim(mipja)||' port='||btrim(mportja)||' dbname='||btrim(mdatabazaja)||' user='||btrim(mmenoja)||' password='||btrim(mhesloja)||''',''SELECT additional_data, '||nahrada||' as idriadok FROM food600.sk__fin WHERE idriadok='||btrim(midriadok::character(20))||''') AS remzmeny(additional_data JSONB, idriadok INTEGER) WHERE sk__fin.idriadok=remzmeny.idriadok AND sk__fin.idriadok='||nahrada;
								END CASE;
								BEGIN
									PERFORM dblink_exec('connstrx',mprikaz1);
								EXCEPTION
									WHEN others	THEN
										RAISE NOTICE 'chyba:%', sqlstate;
										RAISE NOTICE '%',sqlerrm;
										pokracuj:=0;
								END;
								IF pokracuj>0 THEN
									UPDATE food600.sync_set SET id_local_update=midr WHERE idriadok=mojmaster;
								END IF;
							ELSIF btrim(mnazev)='text' THEN
								IF mp1 IS NOT NULL AND btrim(mp1)<>'' THEN
									EXECUTE('SELECT food600.'||btrim(mp1)) INTO nahrada;
								END IF;
								CASE
									WHEN btrim(mtabulka)='karty' AND btrim(mprikaz)='vyr_postup' THEN
										mprikaz1:='UPDATE food600.karty SET vyr_postup=remzmeny.vyr_postup FROM dblink(''hostaddr='||btrim(mipja)||' port='||btrim(mportja)||' dbname='||btrim(mdatabazaja)||' user='||btrim(mmenoja)||' password='||btrim(mhesloja)||''',''SELECT vyr_postup, '||nahrada||' as c_karty FROM food600.karty WHERE c_karty='||btrim(midriadok::character(20))||''') AS remzmeny(vyr_postup TEXT, c_karty INTEGER) WHERE karty.c_karty=remzmeny.c_karty AND karty.c_karty='||nahrada;
								END CASE;
								BEGIN
									PERFORM dblink_exec('connstrx',mprikaz1);
								EXCEPTION
									WHEN others	THEN
										RAISE NOTICE 'chyba:%', sqlstate;
										RAISE NOTICE '%',sqlerrm;
										pokracuj:=0;
								END;
								IF pokracuj>0 THEN
									UPDATE food600.sync_set SET id_local_update=midr WHERE idriadok=mojmaster;
								END IF;
							ELSE
								IF zobrazuj THEN 
									RAISE NOTICE 'idr:%', midr;
									RAISE NOTICE 'mprikaz:%', mprikaz;
								END IF;
								IF mp6 IS NOT NULL AND btrim(mp6)<>'' THEN
									EXECUTE('SELECT food600.'||btrim(mp6)) INTO nahrada;
									mprikaz:=REPLACE(mprikaz,'%P6%',nahrada);
								END IF;
								IF mp5 IS NOT NULL AND btrim(mp5)<>'' THEN
									EXECUTE('SELECT food600.'||btrim(mp5)) INTO nahrada;
									mprikaz:=REPLACE(mprikaz,'%P5%',nahrada);
								END IF;
								IF mp4 IS NOT NULL AND btrim(mp4)<>'' THEN
									EXECUTE('SELECT food600.'||btrim(mp4)) INTO nahrada;
									mprikaz:=REPLACE(mprikaz,'%P4%',nahrada);
								END IF;
								IF mp3 IS NOT NULL AND btrim(mp3)<>'' THEN
									EXECUTE('SELECT food600.'||btrim(mp3)) INTO nahrada;
									mprikaz:=REPLACE(mprikaz,'%P3%',nahrada);
								END IF;
								IF mp2 IS NOT NULL AND btrim(mp2)<>'' THEN
									EXECUTE('SELECT food600.'||btrim(mp2)) INTO nahrada;
									mprikaz:=REPLACE(mprikaz,'%P2%',nahrada);
								END IF;
								IF mp1 IS NOT NULL AND btrim(mp1)<>'' THEN
									EXECUTE('SELECT food600.'||btrim(mp1)) INTO nahrada;
									mprikaz:=REPLACE(mprikaz,'%P1%',nahrada);
								END IF;
								BEGIN
									PERFORM dblink_exec('connstrx',mprikaz);
								EXCEPTION
									WHEN others	THEN
										RAISE NOTICE 'chyba:%', sqlstate;
										RAISE NOTICE '%',sqlerrm;
										midriadokmaster:=null;
										pokracuj:=0;
								END;
								IF pokracuj>0 THEN
									UPDATE food600.sync_set SET id_local_update=midr WHERE idriadok=mojmaster;
								END IF;
							END IF;
						END IF;
					END LOOP;		
					CLOSE zoznamupdatovostatne;	
				END IF;
				IF pokracuj>0 THEN
					mdone := 100000;
					OPEN zoznamremupdatovostatne;
					IF zobrazuj THEN
						RAISE NOTICE 'otvoreny zoznam remupdatov ostatnych tabuliek';
					END IF;
					WHILE (mdone>0) AND (pokracuj>0) LOOP
						FETCH zoznamremupdatovostatne INTO midr, mprikaz, midriadok, mtabulka, mp1, mp2, mp3, mp4, mp5, mp6, mporadie, mnazev;
						IF midr ISNULL THEN
							mdone:=0;
						ELSE
							IF btrim(mnazev)='bytea' THEN
								IF mp1 IS NOT NULL AND btrim(mp1)<>'' THEN
									EXECUTE('SELECT food600.'||btrim(mp1)) INTO nahrada;
								END IF;
								BEGIN
									CASE
										WHEN btrim(mtabulka)='obaly' AND btrim(mprikaz)='obrazok' THEN
											EXECUTE format('UPDATE food600.obaly SET obrazok=remzmeny.obrazok FROM dblink(''connstrx'',''SELECT obrazok, %s as idriadok FROM food600.obaly WHERE idriadok=%s'') AS remzmeny(obrazok BYTEA, idriadok INTEGER) WHERE obaly.idriadok=remzmeny.idriadok AND obaly.idriadok=%s', nahrada::INTEGER, midriadok::INTEGER, nahrada::INTEGER);
										WHEN btrim(mtabulka)='krest' AND btrim(mprikaz)='obrazok' THEN
											EXECUTE format('UPDATE food600.krest SET obrazok=remzmeny.obrazok FROM dblink(''connstrx'',''SELECT obrazok, %s as idriadok FROM food600.krest WHERE idriadok=%s'') AS remzmeny(obrazok BYTEA, idriadok INTEGER) WHERE krest.idriadok=remzmeny.idriadok AND krest.idriadok=%s', nahrada::INTEGER, midriadok::INTEGER, nahrada::INTEGER);
										WHEN btrim(mtabulka)='selmist' AND btrim(mprikaz)='pozadi' THEN
											EXECUTE format('UPDATE food600.selmist SET pozadi=remzmeny.pozadi FROM dblink(''connstrx'',''SELECT pozadi, %s as idriadok FROM food600.selmist WHERE idriadok=%s'') AS remzmeny(pozadi BYTEA, idriadok INTEGER) WHERE selmist.idriadok=remzmeny.idriadok AND selmist.idriadok=%s', nahrada::INTEGER, midriadok::INTEGER, nahrada::INTEGER);
									END CASE;
								EXCEPTION
									WHEN others	THEN
										RAISE NOTICE 'chyba:%', sqlstate;
										RAISE NOTICE '%',sqlerrm;
										pokracuj:=0;
								END;
								IF pokracuj>0 THEN
									UPDATE food600.sync_set SET id_remote_update=midr WHERE idriadok=mojmaster;
								END IF;
							ELSIF btrim(mnazev)='json' THEN
								IF mp1 IS NOT NULL AND btrim(mp1)<>'' THEN
									EXECUTE('SELECT food600.'||btrim(mp1)) INTO nahrada;
								END IF;
								BEGIN
									CASE
										WHEN btrim(mtabulka)='sk__uza' AND btrim(mprikaz)='rep_uzaverka' THEN
											EXECUTE format('UPDATE food600.sk__uza SET rep_uzaverka=remzmeny.rep_uzaverka FROM dblink(''connstrx'',''SELECT rep_uzaverka, %s as idriadok FROM food600.sk__uza WHERE idriadok=%s'') AS remzmeny(rep_uzaverka JSON, idriadok INTEGER) WHERE sk__uza.idriadok=remzmeny.idriadok AND sk__uza.idriadok=%s', nahrada::INTEGER, midriadok::INTEGER, nahrada::INTEGER);
										WHEN btrim(mtabulka)='sk__uza_cis' AND btrim(mprikaz)='rep_uzaverka' THEN
											EXECUTE format('UPDATE food600.sk__uza_cis SET rep_uzaverka=remzmeny.rep_uzaverka FROM dblink(''connstrx'',''SELECT rep_uzaverka, %s as idriadok FROM food600.sk__uza_cis WHERE idriadok=%s'') AS remzmeny(rep_uzaverka JSON, idriadok INTEGER) WHERE sk__uza_cis.idriadok=remzmeny.idriadok AND sk__uza_cis.idriadok=%s', nahrada::INTEGER, midriadok::INTEGER, nahrada::INTEGER);
										WHEN btrim(mtabulka)='sk__txt' AND btrim(mprikaz)='info' THEN
											EXECUTE format('UPDATE food600.sk__txt SET info=remzmeny.info FROM dblink(''connstrx'',''SELECT info, %s as idriadok FROM food600.sk__txt WHERE idriadok=%s'') AS remzmeny(info JSON, idriadok INTEGER) WHERE sk__txt.idriadok=remzmeny.idriadok AND sk__txt.idriadok=%s', nahrada::INTEGER, midriadok::INTEGER, nahrada::INTEGER);
										WHEN btrim(mtabulka)='sk__fin' AND btrim(mprikaz)='additional_data' THEN
											EXECUTE format('UPDATE food600.sk__fin SET info=remzmeny.additional_data FROM dblink(''connstrx'',''SELECT additional_data, %s as idriadok FROM food600.sk__fin WHERE idriadok=%s'') AS remzmeny(additional_data JSON, idriadok INTEGER) WHERE sk__fin.idriadok=remzmeny.idriadok AND sk__fin.idriadok=%s', nahrada::INTEGER, midriadok::INTEGER, nahrada::INTEGER);
									END CASE;
								EXCEPTION
									WHEN others	THEN
										RAISE NOTICE 'chyba:%', sqlstate;
										RAISE NOTICE '%',sqlerrm;
										pokracuj:=0;
								END;
								IF pokracuj>0 THEN
									UPDATE food600.sync_set SET id_remote_update=midr WHERE idriadok=mojmaster;
								END IF;
							ELSIF btrim(mnazev)='jsonb' THEN
								IF mp1 IS NOT NULL AND btrim(mp1)<>'' THEN
									EXECUTE('SELECT food600.'||btrim(mp1)) INTO nahrada;
								END IF;
								BEGIN
									CASE
										WHEN btrim(mtabulka)='sk__uza' AND btrim(mprikaz)='rep_uzaverka' THEN
											EXECUTE format('UPDATE food600.sk__uza SET rep_uzaverka=remzmeny.rep_uzaverka FROM dblink(''connstrx'',''SELECT rep_uzaverka, %s as idriadok FROM food600.sk__uza WHERE idriadok=%s'') AS remzmeny(rep_uzaverka JSONB, idriadok INTEGER) WHERE sk__uza.idriadok=remzmeny.idriadok AND sk__uza.idriadok=%s', nahrada::INTEGER, midriadok::INTEGER, nahrada::INTEGER);
										WHEN btrim(mtabulka)='sk__uza_cis' AND btrim(mprikaz)='rep_uzaverka' THEN
											EXECUTE format('UPDATE food600.sk__uza_cis SET rep_uzaverka=remzmeny.rep_uzaverka FROM dblink(''connstrx'',''SELECT rep_uzaverka, %s as idriadok FROM food600.sk__uza_cis WHERE idriadok=%s'') AS remzmeny(rep_uzaverka JSONB, idriadok INTEGER) WHERE sk__uza_cis.idriadok=remzmeny.idriadok AND sk__uza_cis.idriadok=%s', nahrada::INTEGER, midriadok::INTEGER, nahrada::INTEGER);
										WHEN btrim(mtabulka)='sk__txt' AND btrim(mprikaz)='info' THEN
											EXECUTE format('UPDATE food600.sk__txt SET info=remzmeny.info FROM dblink(''connstrx'',''SELECT info, %s as idriadok FROM food600.sk__txt WHERE idriadok=%s'') AS remzmeny(info JSONB, idriadok INTEGER) WHERE sk__txt.idriadok=remzmeny.idriadok AND sk__txt.idriadok=%s', nahrada::INTEGER, midriadok::INTEGER, nahrada::INTEGER);
										WHEN btrim(mtabulka)='sk__fin' AND btrim(mprikaz)='additional_data' THEN
											EXECUTE format('UPDATE food600.sk__fin SET additional_data=remzmeny.additional_data FROM dblink(''connstrx'',''SELECT additional_data, %s as idriadok FROM food600.sk__fin WHERE idriadok=%s'') AS remzmeny(additional_data JSONB, idriadok INTEGER) WHERE sk__fin.idriadok=remzmeny.idriadok AND sk__fin.idriadok=%s', nahrada::INTEGER, midriadok::INTEGER, nahrada::INTEGER);
									END CASE;
								EXCEPTION
									WHEN others	THEN
										RAISE NOTICE 'chyba:%', sqlstate;
										RAISE NOTICE '%',sqlerrm;
										pokracuj:=0;
								END;
								IF pokracuj>0 THEN
									UPDATE food600.sync_set SET id_remote_update=midr WHERE idriadok=mojmaster;
								END IF;
							ELSIF btrim(mnazev)='text' THEN
								IF mp1 IS NOT NULL AND btrim(mp1)<>'' THEN
									EXECUTE('SELECT food600.'||btrim(mp1)) INTO nahrada;
								END IF;
								BEGIN
									CASE
										WHEN btrim(mtabulka)='karty' AND btrim(mprikaz)='vyr_postup' THEN
											EXECUTE format('UPDATE food600.karty SET vyr_postup=remzmeny.vyr_postup FROM dblink(''connstrx'',''SELECT vyr_postup, %s as c_karty FROM food600.karty WHERE c_karty=%s'') AS remzmeny(vyr_postup TEXT, c_karty INTEGER) WHERE karty.c_karty=remzmeny.c_karty AND karty.c_karty=%s', nahrada::INTEGER, midriadok::INTEGER, nahrada::INTEGER);
									END CASE;
								EXCEPTION
									WHEN others	THEN
										RAISE NOTICE 'chyba:%', sqlstate;
										RAISE NOTICE '%',sqlerrm;
										pokracuj:=0;
								END;
								IF pokracuj>0 THEN
									UPDATE food600.sync_set SET id_remote_update=midr WHERE idriadok=mojmaster;
								END IF;
							ELSE
								IF zobrazuj THEN 
									RAISE NOTICE 'idr:%', midr;
									RAISE NOTICE 'mprikaz:%', mprikaz;
								END IF;
								IF mp6 IS NOT NULL AND btrim(mp6)<>'' THEN
									EXECUTE('SELECT food600.'||btrim(mp6)) INTO nahrada;
									mprikaz:=REPLACE(mprikaz,'%P6%',nahrada);
								END IF;
								IF mp5 IS NOT NULL AND btrim(mp5)<>'' THEN
									EXECUTE('SELECT food600.'||btrim(mp5)) INTO nahrada;
									mprikaz:=REPLACE(mprikaz,'%P5%',nahrada);
								END IF;
								IF mp4 IS NOT NULL AND btrim(mp4)<>'' THEN
									EXECUTE('SELECT food600.'||btrim(mp4)) INTO nahrada;
									mprikaz:=REPLACE(mprikaz,'%P4%',nahrada);
								END IF;
								IF mp3 IS NOT NULL AND btrim(mp3)<>'' THEN
									EXECUTE('SELECT food600.'||btrim(mp3)) INTO nahrada;
									mprikaz:=REPLACE(mprikaz,'%P3%',nahrada);
								END IF;
								IF mp2 IS NOT NULL AND btrim(mp2)<>'' THEN
									EXECUTE('SELECT food600.'||btrim(mp2)) INTO nahrada;
									mprikaz:=REPLACE(mprikaz,'%P2%',nahrada);
								END IF;
								IF mp1 IS NOT NULL AND btrim(mp1)<>'' THEN
									EXECUTE('SELECT food600.'||btrim(mp1)) INTO nahrada;
									mprikaz:=REPLACE(mprikaz,'%P1%',nahrada);
								END IF;
								BEGIN
									EXECUTE mprikaz;
								EXCEPTION
									WHEN others	THEN
										RAISE NOTICE 'chyba:%', sqlstate;
										RAISE NOTICE '%',sqlerrm;
										midriadokmaster:=null;
										pokracuj:=0;
								END;
								IF pokracuj>0 THEN
									UPDATE food600.sync_set SET id_remote_update=midr WHERE idriadok=mojmaster;
								END IF;
							END IF;
						END IF;
					END LOOP;		
					CLOSE zoznamremupdatovostatne;	
				END IF;
				IF pokracuj>0 THEN
					mdone := 100000;
					OPEN zoznamdeletovostatne;
					IF zobrazuj THEN
						RAISE NOTICE 'otvoreny zoznam deletov ostatnych kariet';
					END IF;
					WHILE (mdone>0) AND (pokracuj>0) LOOP
						FETCH zoznamdeletovostatne INTO midr, mprikaz, midriadok, mtabulka, mp1, mp2, mp3, mp4, mp5, mp6, mporadie, mnazev;
						IF midr ISNULL THEN
							mdone:=0;
						ELSE
							IF zobrazuj THEN
								RAISE NOTICE 'idr:%', midr;
								RAISE NOTICE 'mprikaz:%', mprikaz;
							END IF;
							IF mp6 IS NOT NULL AND btrim(mp6)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp6)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P6%',nahrada);
							END IF;
							IF mp5 IS NOT NULL AND btrim(mp5)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp5)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P5%',nahrada);
							END IF;
							IF mp4 IS NOT NULL AND btrim(mp4)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp4)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P4%',nahrada);
							END IF;
							IF mp3 IS NOT NULL AND btrim(mp3)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp3)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P3%',nahrada);
							END IF;
							IF mp2 IS NOT NULL AND btrim(mp2)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp2)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P2%',nahrada);
							END IF;
							IF mp1 IS NOT NULL AND btrim(mp1)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp1)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P1%',nahrada);
							END IF;
							BEGIN
								PERFORM dblink_exec('connstrx',mprikaz);
							EXCEPTION
								WHEN others	THEN
									RAISE NOTICE 'chyba:%', sqlstate;
									RAISE NOTICE '%',sqlerrm;
									midriadokmaster:=null;
									pokracuj:=0;
							END;
							IF pokracuj>0 THEN
								UPDATE food600.sync_set SET id_local_delete=midr WHERE idriadok=mojmaster;
							END IF;
						END IF;
					END LOOP;		
					CLOSE zoznamdeletovostatne;	
				END IF;
				IF pokracuj>0 THEN
					mdone := 100000;
					OPEN zoznamremdeletovostatne;
					IF zobrazuj THEN
						RAISE NOTICE 'otvoreny zoznam remdeletov ostatnych tabuliek';
					END IF;
					WHILE (mdone>0) AND (pokracuj>0) LOOP
						FETCH zoznamremdeletovostatne INTO midr, mprikaz, midriadok, mtabulka, mp1, mp2, mp3, mp4, mp5, mp6, mporadie, mnazev;
						IF midr ISNULL THEN
							mdone:=0;
						ELSE
							IF mp6 IS NOT NULL AND btrim(mp6)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp6)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P6%',nahrada);
							END IF;
							IF mp5 IS NOT NULL AND btrim(mp5)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp5)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P5%',nahrada);
							END IF;
							IF mp4 IS NOT NULL AND btrim(mp4)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp4)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P4%',nahrada);
							END IF;
							IF mp3 IS NOT NULL AND btrim(mp3)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp3)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P3%',nahrada);
							END IF;
							IF mp2 IS NOT NULL AND btrim(mp2)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp2)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P2%',nahrada);
							END IF;
							IF mp1 IS NOT NULL AND btrim(mp1)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp1)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P1%',nahrada);
							END IF;
							BEGIN
								EXECUTE mprikaz;
							EXCEPTION
								WHEN others	THEN
									RAISE NOTICE 'chyba:%', sqlstate;
									RAISE NOTICE '%',sqlerrm;
									midriadokmaster:=null;
									pokracuj:=0;
							END;
							IF pokracuj>0 THEN
								UPDATE food600.sync_set SET id_remote_delete=midr WHERE idriadok=mojmaster;
							END IF;
						END IF;
					END LOOP;		
					CLOSE zoznamremdeletovostatne;	
				END IF;
			END IF;
			
			---- L.L. (10.12.2021)  Samostana tabulka hkredobr, pretoze 1. hkredobr uz obsahoval id_sync, ktore sa inak pouzivalo a 2. nestacilo %M1-M6%
			IF pokracuj>0 THEN
				mdone := 100000;
				OPEN zoznaminsertovhkredobr;
				IF zobrazuj THEN
					RAISE NOTICE 'otvoreny zoznam insertov hkredobr';
				END IF;
				WHILE (mdone>0) AND (pokracuj>0) LOOP
					FETCH zoznaminsertovhkredobr INTO midr, mprikaz, midriadok, mtabulka, mp1, mp2, mp3, mp4, mp5, mp6, mp7, mp8, mporadie, mnazev;
					IF midr ISNULL THEN
						mdone:=0;
					ELSE
					IF zobrazuj THEN 
						RAISE NOTICE 'idr:%', midr;
						RAISE NOTICE 'mprikaz:%', mprikaz;
					END IF;
						IF mp8 IS NOT NULL AND btrim(mp8)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp8)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P8%',nahrada);
						END IF;
						IF mp7 IS NOT NULL AND btrim(mp7)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp7)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P7%',nahrada);
						END IF;
						IF mp6 IS NOT NULL AND btrim(mp6)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp6)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P6%',nahrada);
						END IF;
						IF mp5 IS NOT NULL AND btrim(mp5)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp5)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P5%',nahrada);
						END IF;
						IF mp4 IS NOT NULL AND btrim(mp4)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp4)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P4%',nahrada);
						END IF;
						IF mp3 IS NOT NULL AND btrim(mp3)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp3)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P3%',nahrada);
						END IF;
						IF mp2 IS NOT NULL AND btrim(mp2)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp2)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P2%',nahrada);
						END IF;
						IF mp1 IS NOT NULL AND btrim(mp1)<>'' THEN
							EXECUTE('SELECT food600.'||btrim(mp1)) INTO nahrada;
							mprikaz:=REPLACE(mprikaz,'%P1%',nahrada);
						END IF;
						BEGIN
							SELECT idriadok INTO midriadokmaster FROM (SELECT * FROM dblink('connstrx',mprikaz) as remkarta(idriadok integer)) aaa ;
						EXCEPTION
							WHEN others THEN
								RAISE NOTICE 'chyba:%', sqlstate;
								RAISE NOTICE '%',sqlerrm;
								midriadokmaster:=null;
								pokracuj:=0;
						END;
						IF zobrazuj THEN
							RAISE NOTICE 'midriadokmaster2:%, tabulka:%', midriadokmaster, mtabulka;
						END IF;
						IF midriadokmaster IS NOT NULL THEN
							IF zobrazuj THEN
								RAISE NOTICE 'midriadok:%, tabulka: %',midriadok, mtabulka;
							END IF;
							EXECUTE format('UPDATE %s SET idriadok_master=$1 WHERE idriadok=$2','kredit.'||btrim(mtabulka)) USING midriadokmaster, midriadok;
							UPDATE food600.sync_hkredobr SET prikaz=REPLACE(prikaz,'%P1%',btrim(midriadokmaster::character(20))), p1='' WHERE operacia='D' AND tabulka=btrim(mtabulka) AND idriadok=midriadok;
							UPDATE food600.sync_set SET id_local_insert_hkredobr=midr WHERE idriadok=mojmaster;
						END IF;
					END IF;
				END LOOP;		
				CLOSE zoznaminsertovhkredobr;	
				IF pokracuj>0 THEN
					mdone := 100000;
					OPEN zoznamimportovhkredobr;
					IF zobrazuj THEN
						RAISE NOTICE 'otvoreny zoznam importov hkredobr';
					END IF;
					WHILE (mdone>0) AND (pokracuj>0) LOOP
						FETCH zoznamimportovhkredobr INTO midr, mprikaz, midriadok, mtabulka, mp1, mp2, mp3, mp4, mp5, mp6, mp7, mp8, mporadie, mnazev;
						IF midr ISNULL THEN
							mdone:=0;
						ELSE
							IF zobrazuj THEN
								RAISE NOTICE 'idr:%', midr;
								RAISE NOTICE 'mprikaz:%', mprikaz;
							END IF;
							IF mp8 IS NOT NULL AND btrim(mp8)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp8)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P8%',nahrada);
							END IF;
							IF mp7 IS NOT NULL AND btrim(mp7)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp7)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P7%',nahrada);
							END IF;
							IF mp6 IS NOT NULL AND btrim(mp6)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp6)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P6%',nahrada);
							END IF;
							IF mp5 IS NOT NULL AND btrim(mp5)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp5)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P5%',nahrada);
							END IF;
							IF mp4 IS NOT NULL AND btrim(mp4)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp4)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P4%',nahrada);
							END IF;
							IF mp3 IS NOT NULL AND btrim(mp3)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp3)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P3%',nahrada);
							END IF;
							IF mp2 IS NOT NULL AND btrim(mp2)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp2)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P2%',nahrada);
							END IF;
							IF mp1 IS NOT NULL AND btrim(mp1)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp1)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P1%',nahrada);
							END IF;
							BEGIN
								EXECUTE mprikaz INTO midriadokmaster;
							EXCEPTION
								WHEN others THEN
									RAISE NOTICE 'chyba:%', sqlstate;
									RAISE NOTICE '%',sqlerrm;
									midriadokmaster:=null;
									pokracuj:=0;
							END;
							IF zobrazuj THEN
								RAISE NOTICE 'midriadokmaster2:%, tabulka:%', midriadokmaster, mtabulka;
							END IF;
							IF midriadokmaster IS NOT NULL THEN
								IF zobrazuj THEN
									RAISE NOTICE 'midriadok:%, tabulka: %',midriadok, mtabulka;
								END IF;
								EXECUTE format('UPDATE %s SET idriadok_master=$1 WHERE idriadok=$2','kredit.'||btrim(mtabulka)) USING midriadok, midriadokmaster;
								UPDATE food600.sync_hkredobr SET p1='', prikaz=REPLACE(prikaz,'%P1%', btrim(midriadokmaster::CHARACTER(20))) WHERE tabulka=btrim(mtabulka) AND operacia='D' AND idriadok=midriadok;
								UPDATE food600.sync_set SET id_remote_insert_hkredobr=midr WHERE idriadok=mojmaster;
							END IF;
						END IF;
					END LOOP;		
					CLOSE zoznamimportovhkredobr;	
				END IF;
				IF pokracuj>0 THEN
					mdone := 100000;
					OPEN zoznamupdatovhkredobr;
					IF zobrazuj THEN
						RAISE NOTICE 'otvoreny zoznam updatov hkredobr';
					END IF;
					WHILE (mdone>0) AND (pokracuj>0) LOOP
						FETCH zoznamupdatovhkredobr INTO midr, mprikaz, midriadok, mtabulka, mp1, mp2, mp3, mp4, mp5, mp6, mp7, mp8, mporadie, mnazev;
						IF midr ISNULL THEN
							mdone:=0;
						ELSE
							IF zobrazuj THEN 
								RAISE NOTICE 'idr:%', midr;
								RAISE NOTICE 'mprikaz:%', mprikaz;
							END IF;
							IF mp8 IS NOT NULL AND btrim(mp8)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp8)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P8%',nahrada);
							END IF;
							IF mp7 IS NOT NULL AND btrim(mp7)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp7)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P7%',nahrada);
							END IF;
							IF mp6 IS NOT NULL AND btrim(mp6)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp6)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P6%',nahrada);
							END IF;
							IF mp5 IS NOT NULL AND btrim(mp5)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp5)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P5%',nahrada);
							END IF;
							IF mp4 IS NOT NULL AND btrim(mp4)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp4)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P4%',nahrada);
							END IF;
							IF mp3 IS NOT NULL AND btrim(mp3)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp3)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P3%',nahrada);
							END IF;
							IF mp2 IS NOT NULL AND btrim(mp2)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp2)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P2%',nahrada);
							END IF;
							IF mp1 IS NOT NULL AND btrim(mp1)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp1)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P1%',nahrada);
							END IF;
							BEGIN
								PERFORM dblink_exec('connstrx',mprikaz);
							EXCEPTION
								WHEN others	THEN
									RAISE NOTICE 'chyba:%', sqlstate;
									RAISE NOTICE '%',sqlerrm;
									midriadokmaster:=null;
									pokracuj:=0;
							END;
							IF pokracuj>0 THEN
								UPDATE food600.sync_set SET id_local_update_hkredobr=midr WHERE idriadok=mojmaster;
							END IF;
						END IF;						
					END LOOP;		
					CLOSE zoznamupdatovhkredobr;	
				END IF;
				IF pokracuj>0 THEN
					mdone := 100000;
					OPEN zoznamremupdatovhkredobr;
					IF zobrazuj THEN
						RAISE NOTICE 'otvoreny zoznam remupdatov hkredobr';
					END IF;
					WHILE (mdone>0) AND (pokracuj>0) LOOP
						FETCH zoznamremupdatovhkredobr INTO midr, mprikaz, midriadok, mtabulka, mp1, mp2, mp3, mp4, mp5, mp6, mp7, mp8, mporadie, mnazev;
						IF midr ISNULL THEN
							mdone:=0;
						ELSE
							IF zobrazuj THEN 
								RAISE NOTICE 'idr:%', midr;
								RAISE NOTICE 'mprikaz:%', mprikaz;
							END IF;
							IF mp8 IS NOT NULL AND btrim(mp8)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp8)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P8%',nahrada);
							END IF;
							IF mp7 IS NOT NULL AND btrim(mp7)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp7)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P7%',nahrada);
							END IF;
							IF mp6 IS NOT NULL AND btrim(mp6)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp6)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P6%',nahrada);
							END IF;
							IF mp5 IS NOT NULL AND btrim(mp5)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp5)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P5%',nahrada);
							END IF;
							IF mp4 IS NOT NULL AND btrim(mp4)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp4)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P4%',nahrada);
							END IF;
							IF mp3 IS NOT NULL AND btrim(mp3)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp3)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P3%',nahrada);
							END IF;
							IF mp2 IS NOT NULL AND btrim(mp2)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp2)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P2%',nahrada);
							END IF;
							IF mp1 IS NOT NULL AND btrim(mp1)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp1)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P1%',nahrada);
							END IF;
							BEGIN
								EXECUTE mprikaz;
							EXCEPTION
								WHEN others	THEN
									RAISE NOTICE 'chyba:%', sqlstate;
									RAISE NOTICE '%',sqlerrm;
									midriadokmaster:=null;
									pokracuj:=0;
							END;
							IF pokracuj>0 THEN
								UPDATE food600.sync_set SET id_remote_update_hkredobr=midr WHERE idriadok=mojmaster;
							END IF;
						END IF;
					END LOOP;		
					CLOSE zoznamremupdatovhkredobr;	
				END IF;
				IF pokracuj>0 THEN
					mdone := 100000;
					OPEN zoznamdeletovhkredobr;
					IF zobrazuj THEN
						RAISE NOTICE 'otvoreny zoznam deletov hkredobr';
					END IF;
					WHILE (mdone>0) AND (pokracuj>0) LOOP
						FETCH zoznamdeletovhkredobr INTO midr, mprikaz, midriadok, mtabulka, mp1, mp2, mp3, mp4, mp5, mp6, mp7, mp8, mporadie, mnazev;
						IF midr ISNULL THEN
							mdone:=0;
						ELSE
							IF zobrazuj THEN
								RAISE NOTICE 'idr:%', midr;
								RAISE NOTICE 'mprikaz:%', mprikaz;
							END IF;
							IF mp8 IS NOT NULL AND btrim(mp8)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp8)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P8%',nahrada);
							END IF;
							IF mp7 IS NOT NULL AND btrim(mp7)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp7)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P7%',nahrada);
							END IF;
							IF mp6 IS NOT NULL AND btrim(mp6)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp6)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P6%',nahrada);
							END IF;
							IF mp5 IS NOT NULL AND btrim(mp5)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp5)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P5%',nahrada);
							END IF;
							IF mp4 IS NOT NULL AND btrim(mp4)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp4)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P4%',nahrada);
							END IF;
							IF mp3 IS NOT NULL AND btrim(mp3)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp3)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P3%',nahrada);
							END IF;
							IF mp2 IS NOT NULL AND btrim(mp2)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp2)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P2%',nahrada);
							END IF;
							IF mp1 IS NOT NULL AND btrim(mp1)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp1)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P1%',nahrada);
							END IF;
							BEGIN
								PERFORM dblink_exec('connstrx',mprikaz);
							EXCEPTION
								WHEN others	THEN
									RAISE NOTICE 'chyba:%', sqlstate;
									RAISE NOTICE '%',sqlerrm;
									midriadokmaster:=null;
									pokracuj:=0;
							END;
							IF pokracuj>0 THEN
								UPDATE food600.sync_set SET id_local_delete_hkredobr=midr WHERE idriadok=mojmaster;
							END IF;
						END IF;
					END LOOP;		
					CLOSE zoznamdeletovhkredobr;	
				END IF;
				IF pokracuj>0 THEN
					mdone := 100000;
					OPEN zoznamremdeletovhkredobr;
					IF zobrazuj THEN
						RAISE NOTICE 'otvoreny zoznam remdeletov hkredobr';
					END IF;
					WHILE (mdone>0) AND (pokracuj>0) LOOP
						FETCH zoznamremdeletovhkredobr INTO midr, mprikaz, midriadok, mtabulka, mp1, mp2, mp3, mp4, mp5, mp6, mp7, mp8,  mporadie, mnazev;
						IF midr ISNULL THEN
							mdone:=0;
						ELSE
							IF mp8 IS NOT NULL AND btrim(mp8)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp8)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P8%',nahrada);
							END IF;
							IF mp7 IS NOT NULL AND btrim(mp7)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp7)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P7%',nahrada);
							END IF;
							IF mp6 IS NOT NULL AND btrim(mp6)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp6)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P6%',nahrada);
							END IF;
							IF mp5 IS NOT NULL AND btrim(mp5)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp5)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P5%',nahrada);
							END IF;
							IF mp4 IS NOT NULL AND btrim(mp4)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp4)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P4%',nahrada);
							END IF;
							IF mp3 IS NOT NULL AND btrim(mp3)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp3)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P3%',nahrada);
							END IF;
							IF mp2 IS NOT NULL AND btrim(mp2)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp2)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P2%',nahrada);
							END IF;
							IF mp1 IS NOT NULL AND btrim(mp1)<>'' THEN
								EXECUTE('SELECT food600.'||btrim(mp1)) INTO nahrada;
								mprikaz:=REPLACE(mprikaz,'%P1%',nahrada);
							END IF;
							BEGIN
								EXECUTE mprikaz;
							EXCEPTION
								WHEN others	THEN
									RAISE NOTICE 'chyba:%', sqlstate;
									RAISE NOTICE '%',sqlerrm;
									midriadokmaster:=null;
									pokracuj:=0;
							END;
							IF pokracuj>0 THEN
								UPDATE food600.sync_set SET id_remote_delete_hkredobr=midr WHERE idriadok=mojmaster;
							END IF;
						END IF;
					END LOOP;		
					CLOSE zoznamremdeletovhkredobr;	
				END IF;
			END IF;
			---- L.L. (10.12.2021)  Koniec: Samostana tabulka hkredobr, pretoze 1. hkredobr uz obsahoval id_sync, ktore sa inak pouzivalo a 2. nestacilo %M1-M6%			
			
			PERFORM dblink_disconnect('connstrx');
		END IF;
	END IF;
END IF;
END
$BODY$;
