CREATE FUNCTION "DBA"."get_kp_mall_alus"( in a_tyyp smallint, in a_info char(200) default '', in a_asutnr integer default null, in a_domain char(10) default '', in a_family char(10) default '', in a_subfamily char(10) default '')
RETURNS INTEGER
Begin
  declare ll_alusnr integer;
  if a_info is null then
    set a_info=''
  end if;
  if a_domain is null then
    set a_domain=''
  end if;
  if a_family is null then
    set a_family=''
  end if;
  if a_subfamily is null then
    set a_subfamily=''
  end if;
  if a_asutnr is null then
    select first kp_mall.alusnr into ll_alusnr from kp_mall where kp_mall.tyyp=a_tyyp and (locate(a_info,"kp_mall"."infosis")>0 or "kp_mall"."infosis"='') and Left(a_domain,Len("kp_mall"."domain"))="kp_mall"."domain" and Left(a_family,Len("kp_mall"."family"))="kp_mall"."family" and Left(a_subfamily,Len("kp_mall"."subfamily"))="kp_mall"."subfamily";
  else
    select first kp_mall.alusnr into ll_alusnr from kp_mall where kp_mall.tyyp=a_tyyp and (kp_mall.asutnr=a_asutnr or kp_mall.asutnr is null) and (locate(a_info,"kp_mall"."infosis")>0 or "kp_mall"."infosis"='') and Left(a_domain,Len("kp_mall"."domain"))="kp_mall"."domain" and Left(a_family,Len("kp_mall"."family"))="kp_mall"."family" and Left(a_subfamily,Len("kp_mall"."subfamily"))="kp_mall"."subfamily";
  end if;
  return ll_alusnr;
END