ALTER FUNCTION "dba"."existid_pr_alus"( in a_nimi char(50), in a_order char(3) default null, in a_deeb char(7) default null, in a_partnr integer default null, in a_kree char(7) default null, in a_partnr2 integer default null, 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, in a_alliknr integer default null, in a_ravonr integer default null )
returns integer
begin
  declare ll_alusnr integer;
  select Min(pr_alus.alusnr) into ll_alusnr from pr_alus
    where pr_alus.nimi=a_nimi and
         (deeb=a_deeb or (deeb is null and a_deeb is null)) and
         (partnr=a_partnr or (partnr is null and a_partnr is null)) and
         (kree=a_kree or (kree is null and a_kree is null)) and
         (partnr2=a_partnr2 or (partnr2 is null and a_partnr2 is null)) 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)) and
         (alliknr=a_alliknr or (alliknr is null and a_alliknr is null)) and
         (ravonr=a_ravonr or (ravonr is null and a_ravonr is null));
  if ll_alusnr = 0 or ll_alusnr is null then
    set ll_alusnr=getid_pr_alus();
    insert into pr_alus( alusnr,nimi,"order",deeb,partnr,kree,partnr2,meetnr,kulnr,talnr,artnr,tulnr,objectnr,osaknr,subjectnr,suundnr,taotnr,alliknr,ravonr,partjad,part2jad,deebjad,kreejad,meetjad,kuljad,taljad,artjad,tuljad,objectjad,osakjad,subjectjad,suundjad,taotjad,allikjad,ravojad)
      values( ll_alusnr,a_nimi,a_order,a_deeb,a_partnr,a_kree,a_partnr2,a_meetnr,a_kulnr,a_talnr,a_artnr,a_tulnr,a_objectnr,a_osaknr,a_subjectnr,a_suundnr,a_taotnr,a_alliknr,a_ravonr,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0) ;
  end if;
  return(ll_alusnr)
end