Advertisement
pveselov

agrio merge

Jan 7th, 2015
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. CREATE OR REPLACE FUNCTION public."merge_all02-9A-46-8B-C1-DD"()
  2.  RETURNS void
  3.  LANGUAGE plpgsql
  4. AS $function$
  5.   declare
  6.     agrow RECORD;
  7.     agrmap numeric [];
  8.     agrid numeric;
  9.     ver numeric;
  10.     code varchar;
  11. BEGIN
  12.  
  13.     for agrow in select * from "R_AGRIO02-9A-46-8B-C1-DD"
  14.                  order by tagid, blockid, rowdate, device_type, placement
  15.     loop
  16.  
  17.       if agrow.tagid = 0 then
  18.         code := '-' || agrow.blockid;
  19.       else
  20.         code := agrow.tagid || '-';
  21.       end if;
  22.  
  23.       ver := get_r_lock(code, false);
  24.  
  25.       if ver <= 0 then
  26.         raise exception USING ERRCODE = 'check_violation',
  27.          MESSAGE = 'Can not obtain lock for key code ' || code || ', returned ver is ' || ver;
  28.       end if;
  29.  
  30.       if agrow.version < ver then
  31.         continue;
  32.       end if;
  33.       select merge_agrio(null::numeric, agrow.tagid,
  34.         agrow.blockid, agrow.rowdate,
  35.         agrow.count, agrow.events,
  36.         agrow.devents, agrow.duration,
  37.         agrow.device_type, agrow.placement,
  38.         agrow.unserved, agrow.unconfirmed) into agrid;
  39.       agrmap[agrow.id] = agrid;
  40.     end loop;
  41.  
  42.     for agrow in select * from "R_BRKIOFAULTSS02-9A-46-8B-C1-DD"
  43.     loop
  44.       if agrmap[agrow.agrioid] is null then continue; end if;
  45.       perform merge_brkiofaults(
  46.         agrmap[agrow.agrioid],
  47.         agrow.blockid,
  48.         agrow.name,
  49.         agrow.type,
  50.         agrow.cname,
  51.         agrow.typeid,
  52.         agrow.count,
  53.         agrow.dcount,
  54.         agrow.auto);
  55.     end loop;
  56.  
  57.     delete from "R_BRKIOFAULTS02-9A-46-8B-C1-DD";
  58.     delete from "R_AGRIO02-9A-46-8B-C1-DD";
  59.  
  60. END;
  61. $function$
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement