PL / SQL:无效数字错误

时间:2018-05-23 06:25:47

标签: sql oracle

我正在创建一个程序,我通过重复运行以下查询来收集数据。

SELECT ATTRIBUTE_VALUE,
       COUNT(src1) CNT1,
       COUNT(src2) CNT2 
FROM   (
  SELECT a.ATTRIBUTE_VALUE,
         1 src1,
         TO_NUMBER(NULL) src2 
  FROM   (
    SELECT DECODE(
             L,
             1, IP_ADDRESS,
             DECODE(
               L,
               2, IP_SUBNET_MASK,
               DECODE(
                 L,
                 3, IP_DEFAULT_GATEWAY
               )
             )
           ) ATTRIBUTE_VALUE 
    FROM   ( SELECT LEVEL L FROM DUAL X CONNECT BY LEVEL <= 3 ), 
           REUT_LOAD_IP_ADDRESSES 
    WHERE  LIP_IPT_NAME = 'CE' 
    AND    IP_LNT_ID IN ( 
             SELECT LNT_ID 
             FROM REUT_LOAD_NTN 
             WHERE LNT_ID IN ( 
               SELECT RLPN.LPN_LNT_ID 
               FROM REUT_LOAD_PI_NTN RLPN 
               WHERE LPN_LPI_ID IN ( 
                 SELECT RLPI.LPI_ID 
                 FROM REUT_LOAD_PAC_INS RLPI 
                 WHERE RLPI.LPI_DATE_ADDED IN ( 
                   SELECT MAX(RLPI2.LPI_DATE_ADDED) 
                   FROM REUT_LOAD_PAC_INS RLPI2 
                   WHERE RLPI2.PI_JOB_ID = P_ORDER_ID 
                 ) 
               ) 
             ) 
             AND IP_CEASE_DATE IS NULL 
             AND LNT_SERVICE_INSTANCE = 'PRIMARY' 
           ) 

它在SQL开发人员中运行良好,但在作为过程执行时,我在

处获得INVALID NUMBER ERROR(ORA-01722:无效数字) 代码中的

AND IP_LNT_ID IN ( SELECT LNT_ID。 我能得到任何帮助吗?

2 个答案:

答案 0 :(得分:0)

错误很清楚。您正在将数字与另一种类型的值进行比较。

示例:

SELECT 'x'
  FROM DUAL
 WHERE 1 IN (SELECT 'a'
               FROM DUAL)

这意味着IP_LNT_IDLNT_IDLPN_LNT_IDLPI_ID必须为NUMBERLPI_DATE_ADDEDLPI_DATE_ADDED都应该是日期或时间戳。

如果无法做到这一点,您可以将所有内容都比作char

SELECT ATTRIBUTE_VALUE, COUNT(src1) CNT1, COUNT(src2) CNT2 
        FROM (SELECT a.ATTRIBUTE_VALUE, 1 src1, TO_NUMBER(NULL) src2 
            FROM (SELECT 
                    DECODE(L,1,IP_ADDRESS,DECODE(L,2,IP_SUBNET_MASK,DECODE(L,3,IP_DEFAULT_GATEWAY) ) ) ATTRIBUTE_VALUE 
                    FROM 
                    ( 
                        SELECT LEVEL L FROM DUAL X CONNECT BY LEVEL <= 3 
                    ), 
                    REUT_LOAD_IP_ADDRESSES 
                    WHERE LIP_IPT_NAME = 'CE' 
                    AND to_char(IP_LNT_ID) IN ( 
                        SELECT LNT_ID 
                        FROM REUT_LOAD_NTN 
                        WHERE to_char(LNT_ID) IN ( 
                                SELECT RLPN.LPN_LNT_ID 
                                FROM REUT_LOAD_PI_NTN RLPN 
                                WHERE to_char(LPN_LPI_ID) IN ( 
                                    SELECT RLPI.LPI_ID 
                                    FROM REUT_LOAD_PAC_INS RLPI 
                                    WHERE to_char(RLPI.LPI_DATE_ADDED) IN ( 
                                        SELECT MAX(RLPI2.LPI_DATE_ADDED) 
                                        FROM REUT_LOAD_PAC_INS RLPI2 
                                        WHERE RLPI2.PI_JOB_ID = P_ORDER_ID 
                                    ) 
                            ) 
                    ) 
                    AND IP_CEASE_DATE IS NULL 
                    AND LNT_SERVICE_INSTANCE = 'PRIMARY' 
            ) 

但是这应该避免任何代价。不幸的是,有时候我们不得不经常欺骗我们现有的基础设施; - )

答案 1 :(得分:0)

您需要确保:

REUT_LOAD_IP_ADDRESSES.IP_LNT_ID

REUT_LOAD_NTN.LNT_ID

具有相同的数据类型或转换/转换其中一个或另一个,以便它们具有相同的数据类型。

还有其他一些问题:

  1. 您有汇总和非汇总的值:

    SELECT ATTRIBUTE_VALUE,
           COUNT(src1) CNT1,
           COUNT(src2) CNT2 
    FROM   ( ... )
    

    没有GROUP BY条款。

  2. src2TO_NUMBER(NULL)NULLCOUNT(NULL)始终为0,因此您的查询为:

    SELECT ATTRIBUTE_VALUE,
           COUNT(src1) CNT1,
           0 CNT2
    ...
    
  3. 此代码:

    SELECT DECODE(
             L,
             1, IP_ADDRESS,
             DECODE(
               L,
               2, IP_SUBNET_MASK,
               DECODE(
                 L,
                 3, IP_DEFAULT_GATEWAY
               )
             )
           ) ATTRIBUTE_VALUE 
    FROM   ( SELECT LEVEL L FROM DUAL X CONNECT BY LEVEL <= 3 ), 
           REUT_LOAD_IP_ADDRESSES 
    

    可以改写为:

    SELECT DECODE(
             L,
             1, IP_ADDRESS,
             2, IP_SUBNET_MASK,
             3, IP_DEFAULT_GATEWAY
           ) ATTRIBUTE_VALUE 
    FROM   ( SELECT LEVEL L FROM DUAL X CONNECT BY LEVEL <= 3 ), 
           REUT_LOAD_IP_ADDRESSES 
    

    或者,没有加入:

    SELECT attribute_value
    FROM   REUT_LOAD_IP_ADDRESSES
    UNPIVOT ( attribute_value FOR L IN (
      IP_ADDRESS         AS 1,
      IP_SUBNET_MASK     AS 2,
      IP_DEFAULT_GATEWAY AS 3
    ) )
    
  4. 最里面的查询:

    SELECT RLPI.LPI_ID 
    FROM REUT_LOAD_PAC_INS RLPI 
    WHERE RLPI.LPI_DATE_ADDED IN ( 
      SELECT MAX(RLPI2.LPI_DATE_ADDED) 
      FROM REUT_LOAD_PAC_INS RLPI2 
      WHERE RLPI2.PI_JOB_ID = P_ORDER_ID 
    ) 
    

    内部查询仅限于RLPI2.PI_JOB_ID = P_ORDER_ID,但外部查询之间没有相关性,因此您可以检索与P_ORDER_ID不匹配的结果,但恰好与匹配的行具有相同的日期