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