CREATE OR REPLACE FUNCTION public."merge_all02-9A-46-8B-C1-DD"() RETURNS void LANGUAGE plpgsql AS $function$ declare agrow RECORD; agrmap numeric []; agrid numeric; ver numeric; code varchar; BEGIN for agrow in select * from "R_AGRIO02-9A-46-8B-C1-DD" order by tagid, blockid, rowdate, device_type, placement loop if agrow.tagid = 0 then code := '-' || agrow.blockid; else code := agrow.tagid || '-'; end if; ver := get_r_lock(code, false); if ver <= 0 then raise exception USING ERRCODE = 'check_violation', MESSAGE = 'Can not obtain lock for key code ' || code || ', returned ver is ' || ver; end if; if agrow.version < ver then continue; end if; select merge_agrio(null::numeric, agrow.tagid, agrow.blockid, agrow.rowdate, agrow.count, agrow.events, agrow.devents, agrow.duration, agrow.device_type, agrow.placement, agrow.unserved, agrow.unconfirmed) into agrid; agrmap[agrow.id] = agrid; end loop; for agrow in select * from "R_BRKIOFAULTSS02-9A-46-8B-C1-DD" loop if agrmap[agrow.agrioid] is null then continue; end if; perform merge_brkiofaults( agrmap[agrow.agrioid], agrow.blockid, agrow.name, agrow.type, agrow.cname, agrow.typeid, agrow.count, agrow.dcount, agrow.auto); end loop; delete from "R_BRKIOFAULTS02-9A-46-8B-C1-DD"; delete from "R_AGRIO02-9A-46-8B-C1-DD"; END; $function$