CREATE OR REPLACE FUNCTION "&1".GET_ABS_CUSTOMER_NUMBER RETURN NUMBER IS
  CUSTOMERNUMBER NUMBER;
BEGIN
  WITH CUSTNUMBER AS
   (SELECT C.CUSTOMERNUMBER CUSTNR,
           RANK() OVER(ORDER BY C.CUSTOMERNUMBER) AS R,
           MAX(C.CUSTOMERNUMBER) OVER() MAXCUSTNR
      FROM ( SELECT CUST.CUSTOMERNUMBER FROM "&3".CUSTOMER CUST UNION ALL 
             SELECT ERP.CUSTOMERNUMBER FROM "&3".ERP_I_CUSTOMER ERP) C )   
  SELECT MIN((CASE
               WHEN CUSTNUMBER.CUSTNR > CUSTNUMBER.R THEN
                CUSTNUMBER.R
               ELSE
                CUSTNUMBER.MAXCUSTNR + 1
             END))
    INTO CUSTOMERNUMBER
    FROM CUSTNUMBER;
  RETURN CUSTOMERNUMBER;
END GET_ABS_CUSTOMER_NUMBER;
/
exit;