CREATE OR REPLACE FUNCTION "&1".GET_ABS_CUSTOMER_NUMBER RETURN NUMBER IS
   --version 0.1
  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;
  IF CUSTOMERNUMBER IS NULL THEN
    CUSTOMERNUMBER := 1;
  END IF; 
  RETURN CUSTOMERNUMBER;
END GET_ABS_CUSTOMER_NUMBER;
/
exit;