我收到无效的号码错误

时间:2018-02-01 19:59:42

标签: oracle plsql

SET SERVEROUTPUT ON    
DECLARE
    lv_comp_msr VARCHAR2(20000);
 BEGIN
     WITH msr AS
        (SELECT REGEXP_SUBSTR ('02,03,04,09,12', '[^,]+',1,LEVEL) AS msr_id
           FROM DUAL
          CONNECT BY REGEXP_SUBSTR ('02,03,04,09,12', '[^,]+',1,LEVEL) IS NOT NULL    ) 
     SELECT listagg (measure_id, ',') WITHIN GROUP (    ORDER BY measure_id) AS MEASURE_ID 
     INTO lv_comp_msr
    FROM  
          (SELECT measure_id FROM irp_measures_def   ) 
    WHERE measure_id IN  (SELECT listagg (msr_id, ',') WITHIN GROUP (ORDER BY msr_id) msr_id
                          FROM msr   ) 
      --and COMP_MSR_FLAG is null
   ;   
   DBMS_OUTPUT.put_line('lv_comp_msr=' || lv_comp_msr); 
END;

1 个答案:

答案 0 :(得分:1)

listagg()生成字符串。在WHERE子句中,您将其与measure_id进行比较,我将猜测它是数字。您正在比较数字和字符串,因此Oracle执行隐式数据类型转换。但是,聚合的以逗号分隔的字符串当然不能转换为数字,因此会出现ORA-01722错误。

很容易避免错误。你有一个WITH子句,它产生一个数字表:为什么你首先应用listagg()?您只需使用子查询分解的输出:

 WHERE measure_id IN  (SELECT msr_idFROM msr   )