ALTER FUNCTION "DBA"."existid_osaklist"( in a_klass char(16), in a_nimi char(100) default 'Uus osakond' ) 
returns integer
begin
  declare ll_osaknr integer;
  set a_klass = "left"(a_klass+space(16),16);
  select first osaklist.osaknr into ll_osaknr from osaklist where osaklist.osak = a_klass order by osaklist.kell desc;
  if ll_osaknr = 0 or ll_osaknr is null then
    insert into osaklist( osak,nimi ) values( a_klass, a_nimi ) ;
    select osaklist.osaknr into ll_osaknr from osaklist where osaklist.osak = a_klass
  end if;
  return(ll_osaknr)
end