CREATE FUNCTION "DBA"."get_eak_earve_asutnr"( in a_id_dk_earve integer )
RETURNS INTEGER
BEGIN
  declare ll_asutnr integer;
  declare ls_regnumber char(11);
  declare ls_name char(50);
  declare ls_code char(9);
  select if dk_earve.sisse is null or dk_earve.sisse=0 then
           if dk_costreport."id" is null then
             get_eak_asutnr(Left(dk_earve.sellerregnumber,11),Left(dk_earve.sellername,50),Left(Trim(dk_earve.sellercode),9))
           else
             get_eak_asutnr(Left(dk_costreport.sellerregnumber,11),Left(dk_costreport.sellername,50),Left(Trim(dk_costreport.sellercode),9))
           endif
         else
           get_eak_asutnr(Left(dk_earve.buyerregnumber,11),Left(dk_earve.buyername,50),Left(Trim(dk_earve.buyercode),9))
         endif
    into ll_asutnr
    from dk_earve left outer join dk_costreport on dk_earve.id_dk_costreport=dk_costreport."id"
    where dk_earve."id"= a_id_dk_earve;
  return ll_asutnr;
END