ALTER FUNCTION "DBA"."existid_kullist"( in a_klass char(16), in a_nimi char(100) default 'Uus kulu objekt' ) 
returns integer
begin
  declare ll_kulnr integer;
  set a_klass = "left"(a_klass+space(16),16);
  select first kullist.kulnr into ll_kulnr from kullist where kullist.ptk = a_klass order by kullist.kell desc;
  if ll_kulnr = 0 or ll_kulnr is null then
    insert into kullist( ptk,nimi ) values( a_klass, a_nimi ) ;
    select kullist.kulnr into ll_kulnr from kullist where kullist.ptk = a_klass
  end if;
  return(ll_kulnr)
end