ALTER FUNCTION "DBA"."existid_ametlist"( in a_amet char(10) default '',in a_nimi char(50) default 'Uus amet' ) 
returns integer
begin
  declare ll_person integer;
  if a_amet is null then
    set a_amet = ''
  end if;
  if Length(Trim(a_amet)) > 0 then
    select Min(ametlist.person) into ll_person from ametlist where ametlist.amet = a_amet and ametlist.nimi = a_nimi
  else
    select Min(ametlist.person) into ll_person from ametlist where ametlist.nimi = a_nimi
  end if;
  if ll_person = 0 or ll_person is null then
    insert into ametlist( amet,nimi ) values( a_amet,a_nimi ) ;
    select Min(ametlist.person) into ll_person from ametlist where ametlist.amet = a_amet and ametlist.nimi = a_nimi
  end if;
  return(ll_person)
end