如何修改下面的oracle查询以避免ORA-06502错误?

时间:2017-03-03 05:28:31

标签: sql oracle plsql

我有以下用户定义的表:

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

我也有以下功能:

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;

我有以下两个表:(附图像Tables

现在,我正在运行以下查询:

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';

但我收到以下错误:

  

ORA-06502:PL / SQL:数字或值错误:字符串缓冲区也是如此   小ORA-06512:第1行。

我相信我需要修改TO_STRINGS函数或查询;任何帮助都会得到很高的评价。

提前致谢。

1 个答案:

答案 0 :(得分:0)

基本上我在这里理解的是要求是基于人获得一组位置,即基于分隔符的聚合列表。这可以通过LISTAGG函数完成(如果Oracle版本等效或者> 11g)或者可以使用WM_CONCAT(注意这是来自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;