create or replace trigger denik_kod
after update or insert
on denik for each row
declare
  vloz_dkp exception;
  Pragma Exception_init (vloz_dkp,-20300);
  vloz_rezie exception;
  Pragma Exception_init (vloz_rezie,-20301);

  err_dph exception;
  Pragma Exception_init (err_dph,-4001);
  err_platba exception;
  Pragma Exception_init (err_platba,-4002);
  err_vp exception;
  Pragma Exception_init (err_vp,-4003);
  err_typ exception;
  Pragma Exception_init (err_typ,-4004);

  cursor cdph is select dph from ini where dph is NOT NULL;
  cursor cplatba is select platba from ini where platba is NOT NULL;
  cursor ctyp is select typ from ini where typ is NOT NULL;  
  cursor cvp is select vp from ini where vp is NOT NULL;

  cislo integer;
  pismo	varchar2(3);

begin
if :new.KOD=1 then
  raise vloz_dkp;
end if;
if :new.KOD=4 then
  raise vloz_rezie;
end if;
--dynamicky
open cdph;
loop
  fetch cdph into cislo;
  if cdph%NotFound then 
    raise err_dph;
  end if;
  exit when cislo=:new.DPH;
end loop;
open cplatba;
loop
  fetch cplatba into pismo;
  if cplatba%NotFound then 
    raise err_platba;
  end if;
  exit when pismo=:new.platba;
end loop;
open cvp;
loop
  fetch cvp into pismo;
  if cvp%NotFound then 
    raise err_vp;
  end if;
  exit when pismo=:new.vp;
end loop;
open ctyp;
loop
  fetch ctyp into pismo;
  if ctyp%NotFound then 
    raise err_typ;
  end if;
  exit when pismo=:new.typ;
end loop;
--staticky
--if :new.DPH not IN (0,5,22) then
--  raise err_dph;
--end if;
--if :new.PLATBA not IN ('H','h','u','U','V','v') then
--  raise err_platba;
--end if;
--if :new.VP not IN ('v','V','p','P') then
-- raise err_vp;
--end if;
--if :new.Typ not IN ('fak','zfa','par','vyp','FAK','ZFA','PAR','VYP','Fak','Zfa','ZFa','Par','Vyp') then
--  raise err_typ;
--end if;
end;
/
