CREATE OR REPLACE FUNCTION tr_raktar_engedely_frissitese()
  RETURNS trigger AS
'begin
   INSERT INTO _raktar_engedely
      (munkatars_kod, raktar_kod)
   Select munkatars.kod, raktar_nevek.kod
     FROM munkatars CROSS JOIN raktar_nevek
     left outer
     join _raktar_engedely
     on (munkatars.kod = _raktar_engedely.munkatars_kod) and
        (raktar_nevek.kod = _raktar_engedely.raktar_kod)
     where _raktar_engedely.munkatars_kod is null
        AND raktar_nevek.kod <> 0
        AND raktar_nevek.hasznalatban = 1;
   return new;
end;'
  LANGUAGE plpgsql VOLATILE
  COST 100;
  
CREATE TRIGGER trigger_munkatars_ujtete2
  AFTER INSERT
  ON munkatars
  FOR EACH ROW
  EXECUTE PROCEDURE tr_raktar_engedely_frissitese();
  
CREATE TRIGGER trigger_raktar_ujtetel
  AFTER INSERT
  ON mozgasnem
  FOR EACH ROW
  EXECUTE PROCEDURE tr_raktar_engedely_frissitese();