ALTER FUNCTION "DBA"."existid_kblist_protsent"(in a_protsent numeric(10,3) default null)
returns integer
begin
  declare ll_kbnr integer;
  declare ls_nimi char(25);
  if a_protsent is null then
    select first kblist.kbnr into ll_kbnr from kblist where kblist.protsent is null order by kblist.protsent asc,kblist.vaikimisi desc
  else
    select first kblist.kbnr into ll_kbnr from kblist where kblist.protsent=a_protsent order by kblist.protsent asc,kblist.vaikimisi desc
  end if;
  if ll_kbnr=0 or ll_kbnr is null then
    set ll_kbnr=getid_kblist();
    if ll_kbnr>0 then
      if a_protsent is null then
        set ls_nimi='NULL'
      else
        set ls_nimi=convert(char(25),a_protsent)+'%'
      end if;
      insert into kblist(kbnr,nimi,protsent) values(ll_kbnr,ls_nimi,a_protsent);
    else
      set ll_kbnr=null;
    end if;
  end if;
  return(ll_kbnr)
end