ALTER FUNCTION "DBA"."existid_alliklist"( in a_klass char(16), in a_nimi char(100) default 'Uus allikas' ) 
returns integer
begin
  declare ll_alliknr integer;
  set a_klass = "left"(a_klass+space(16),16);
  select first alliklist.alliknr into ll_alliknr from alliklist where alliklist.allik = a_klass order by alliklist.kell desc;
  if ll_alliknr = 0 or ll_alliknr is null then
    insert into alliklist( allik,nimi ) values( a_klass, a_nimi ) ;
    select alliklist.alliknr into ll_alliknr from alliklist where alliklist.allik = a_klass
  end if;
  return(ll_alliknr)
end