简体   繁体   中英

How to modify the below oracle query to avoid ORA-06502 error?

I have the below user defined table:

CREATE OR REPLACE TYPE number_ntt AS TABLE OF varchar2(500);

Also I have the below function:

CREATE OR REPLACE FUNCTION TO_STRINGS (
                    nt_in        IN number_ntt,
                  delimiter_in IN VARCHAR2 DEFAULT ','
                    ) RETURN VARCHAR2 IS
      v_idx PLS_INTEGER;
      v_str VARCHAR2(32767);
      v_dlm VARCHAR2(10);    
   BEGIN    
      v_idx := nt_in.FIRST;
      WHILE v_idx IS NOT NULL LOOP
         v_str := v_str || v_dlm || nt_in(v_idx);
         v_dlm := delimiter_in;
         v_idx := nt_in.NEXT(v_idx);
      END LOOP;    
      RETURN v_str;    
   END TO_STRINGS;
/
EXIT;

I have the below two tables:(Image attached 表 )

Now, I am running the below query:

SELECT A.PERSONNUMBER,
(select TO_STRINGS
(CAST(COLLECT(C.LOCATIONNAME) as number_ntt)
) AS cnt 
from client.Tab_EMPLOYEES B JOIN client.Tab_Locations C ON B.MOBILELOCATIONID = C.MOBILELOCATIONID 
WHERE B.PERSONNUMBER = A.PERSONNUMBER)  as Assignedlocations
FROM client.Tab_EMPLOYEES A WHERE A.MOBILELOCATIONID = '100';

But I am getting the below error:

ORA-06502:PL/SQL:numeric or value error:character string buffer too small ORA-06512:at line 1.

I believe either I need to modify the TO_STRINGS function or the query; any help will be highly appretiated.

Thanks in advance.

Basically what i understand here is the requirement is to get a set of locations based on person ie a aggregated list based on delimiter. That can be done by LISTAGG function (if Oracle version is equivalent or > 11g) or WM_CONCAT can be used (note this is undocumented function from Oracle)

CREATE TABLE AVR_1
  (SR NUMBER, A1 NUMBER_NTT
  )NESTED TABLE A1 STORE AS A1_TAB ;

INSERT INTO AVR_1 VALUES
  ( 1,NUMBER_NTT(1,5,2)
  );

INSERT INTO AVR_1 VALUES
  ( 2,NUMBER_NTT(7,6)
  );


    --Customized function to get output

    CREATE OR REPLACE
  FUNCTION TO_STR(
      P_IN in number,
      p_delimiter IN VARCHAR2 DEFAULT ',')
    RETURN VARCHAR2
  AS
    lv_out VARCHAR2(32676);
  BEGIN
    FOR I IN
    (SELECT a.SR,
      B.column_value val
    FROM AVR_1 a,
      table(a.A1) B
    WHERE a.SR = P_IN
    )
    LOOP
      LV_OUT:=LV_OUT||p_delimiter||I.VAL;
    end LOOP;
    RETURN substr(lv_out,2,length(lv_out));
  EXCEPTION
  WHEN OTHERS THEN
    RETURN ' ';
  END;


--For result

SELECT TO_STR(1,',') FROM DUAL;  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM