ALTER FUNCTION "dba"."existid_ea_alus"( in a_nimi char(50), in a_meetnr integer default null, in a_kulnr integer default null, in a_talnr integer default null, in a_artnr integer default null, in a_tulnr integer default null, in a_objectnr integer default null, in a_osaknr integer default null, in a_subjectnr integer default null, in a_suundnr integer default null, in a_taotnr integer default null)
returns integer
begin
  declare ll_alusnr integer;
  select Min(ea_alus.alusnr) into ll_alusnr from ea_alus
    where ea_alus.nimi=a_nimi and
         (meetnr=a_meetnr or (meetnr is null and a_meetnr is null)) and
         (kulnr=a_kulnr or (kulnr is null and a_kulnr is null)) and
         (talnr=a_talnr or (talnr is null and a_talnr is null)) and
         (artnr=a_artnr or (artnr is null and a_artnr is null)) and
         (tulnr=a_tulnr or (tulnr is null and a_tulnr is null)) and
         (objectnr=a_objectnr or (objectnr is null and a_objectnr is null)) and
         (osaknr=a_osaknr or (osaknr is null and a_osaknr is null)) and
         (subjectnr=a_subjectnr or (subjectnr is null and a_subjectnr is null)) and
         (suundnr=a_suundnr or (suundnr is null and a_suundnr is null)) and
         (taotnr=a_taotnr or (taotnr is null and a_taotnr is null));
  if ll_alusnr = 0 or ll_alusnr is null then
    set ll_alusnr=getid_ea_alus();
    insert into ea_alus( alusnr,nimi,meetnr,kulnr,talnr,artnr,tulnr,objectnr,osaknr,subjectnr,suundnr,taotnr,meetjad,kuljad,taljad,artjad,tuljad,objectjad,osakjad,subjectjad,suundjad,taotjad)
      values( ll_alusnr,a_nimi,a_meetnr,a_kulnr,a_talnr,a_artnr,a_tulnr,a_objectnr,a_osaknr,a_subjectnr,a_suundnr,a_taotnr,0,0,0,0,0,0,0,0,0,0) ;
  end if;
  return(ll_alusnr)
end