ALTER PROCEDURE "dba"."pr_kanne_add"( in a_kuu smallint, in a_aasta smallint, in a_nr integer, in a_id_prog smallint)
BEGIN
  declare ll_skoop integer;
  declare li_allow smallint;
  select if pr_konfm.yld_pr_kanne_add=1 and pr_konfm.yld_fikskuup<=ymd(a_aasta,a_kuu,1) then 1 else 0 endif into li_allow from pr_konfm where id=1;
  if li_allow=1 then
    if a_id_prog=5 then
      set ll_skoop=a_nr
    else
      set ll_skoop=get_skoop(a_id_prog,a_nr);
    end if;
    if not exists(select 1 from pr_import where pr_import.kuu=a_kuu and pr_import.aasta=a_aasta and pr_import.skoop=ll_skoop and pr_import.id_prog=a_id_prog) then
      insert into pr_import(kuu,aasta,skoop,id_prog) values (a_kuu,a_aasta,ll_skoop,a_id_prog)
    end if
  end if
END