ALTER FUNCTION "dba"."if_exists_asulist"( in a_regnr char(11),in a_nimi char(100),in a_enimi char(50) default '')
returns integer
begin
  declare ll_return integer;
  set ll_return=0;
  if Length(Trim(a_regnr)) = 0 then
    set a_regnr=null
  end if;
  if Length(Trim(a_enimi)) = 0 then
    set a_enimi=null
  end if;
  if Length(Trim(a_nimi)) > 0 then
    if exists( select 1 from asulist where (reg_nr=a_regnr or a_regnr is null) and nimi=a_nimi and (enimi=a_enimi or a_enimi is null)) then
      set ll_return=1
    end if
  end if;
  if ll_return=0 and a_regnr is not null then
    if exists( select 1 from asulist where reg_nr=a_regnr) then
      set ll_return=1
    end if
  end if;
  if ll_return is null then
    set ll_return=0
  end if;
  return(ll_return)
end