ALTER FUNCTION "DBA"."existid_tallist"( in a_klass char(16), in a_nimi char(100) default 'Uus tegevusala' ) 
returns integer
begin
  declare ll_talnr integer;
  set a_klass = "left"(a_klass+space(16),16);
  select first tallist.talnr into ll_talnr from tallist where tallist.osa = a_klass order by tallist.kell desc;
  if ll_talnr = 0 or ll_talnr is null then
    insert into tallist( osa,nimi ) values( a_klass, a_nimi ) ;
    select tallist.talnr into ll_talnr from tallist where tallist.osa = a_klass
  end if;
  return(ll_talnr)
end