ALTER FUNCTION "dba"."if_exists_dk_earve_invoiceid"( in a_invoiceid char(100), in a_reg_nr char(50) default null, in a_invoicenumber char(50) default '' )
RETURNS INTEGER
BEGIN
  DECLARE li_return INTEGER;
  if a_invoiceid is null or Length(Trim(a_invoiceid))=0 then
    set li_return=-1;
  else
    if a_invoicenumber is null then
      set a_invoicenumber=''
    end if;
    if exists(select 1 from "dk_earve" where "dk_earve"."invoiceid"=a_invoiceid and (a_reg_nr is null or "dk_earve"."SellerRegNumber" is null or "dk_earve"."SellerRegNumber"=a_reg_nr) and "dk_earve"."invoicenumber"=a_invoicenumber) then
      set li_return=1;
    else
      set li_return=0;
    end if
  end if;
  RETURN li_return;
END