ALTER FUNCTION "dba"."get_arvesaaja_meil2"( in a_asutnr integer, in a_saajanr integer )
returns char(50)
BEGIN
  declare ls_return char(50);
  if exists(select 1 from dk_konfm where dk_konfm.id=1 and dk_konfm.yld_arve_saajale=1) then
    select if eakchannel2 is not null and eakchannel2=5 and asulist.meil2 is not null and Length(asulist.meil2)>0 then asulist.meil2 else if eakchannel2 is not null and eakchannel2=3 and asulist.meil is not null and Length(asulist.meil)>0 then asulist.meil endif endif into ls_return from asulist where asulist.asutnr=a_saajanr;
  end if;
  if ls_return is null or Length(Trim(ls_return))=0 then
    select if eakchannel2 is not null and eakchannel2=5 and asulist.meil2 is not null and Length(asulist.meil2)>0 then asulist.meil2 else if eakchannel2 is not null and eakchannel2=3 and asulist.meil is not null and Length(asulist.meil)>0 then asulist.meil endif endif into ls_return from asulist where asulist.asutnr=a_asutnr;
  end if;
  if ls_return is null or Length(Trim(ls_return))=0 then
    set ls_return=''
  end if;
  return ls_return;
END