执行OR时,在Oracle中抛出无效的数字错误

时间:2013-09-26 08:36:20

标签: database oracle oracle10g

我的表结构是..

A_TEXT(ID number,name varchar2)
POLICY_INFO (PI_ID number,relation_id varchar2)

我有这样的查询。

SELECT * FROM A_TEXT A  WHERE   a.name  = 'test' OR  a.ID IN 
            (SELECT  TO_NUMBER(RELATION_ID )
             FROM POLICY_INFO 
             )  

当我尝试执行查询时,我得到了无效数字异常。如果你修改了一点点查询:

SELECT * FROM A_TEXT A  WHERE a.name  = 'test' OR  TO_CHAR(a.ID) IN 
           (SELECT  RELATION_ID
             FROM POLICY_INFO 
             )  

执行得很好。

2 个答案:

答案 0 :(得分:1)

可能的原因是列RELATION_ID包含非数字字符(Anything apart from 0-9, NULL)

请记住,所有NUMBER都可以转换为VARCHAR,反之亦然。

要检查列RELATION_ID,请使用以下函数并验证非数字字符串。

CREATE OR REPLACE FUNCTION isnumeric(p_string in varchar2)
RETURN BOOLEAN
AS
    l_number number;
BEGIN
    l_number := p_string;
    RETURN TRUE;
EXCEPTION
    WHEN OTHERS THEN
        RETURN FALSE;
END;
/

关注:

WITH POLICY_INFO
    AS (SELECT
             1 AS PI_ID,
             '1' AS RELATION_ID
        FROM
             DUAL),
    A_TEXT
    AS (SELECT
             1 AS ID,
             'testA' AS NAME
        FROM
             DUAL
        UNION ALL
        SELECT
             2 AS ID,
             'test' AS NAME
        FROM
             DUAL)
SELECT
      *
FROM
      A_TEXT A
WHERE
      A.NAME = 'test'
      OR A.ID IN (SELECT TO_NUMBER ( RELATION_ID ) FROM POLICY_INFO);

这很好用。请检查一下。如果您仍然遇到问题,请告诉我们完整的数据集

答案 1 :(得分:0)

您的数据库列中有非数字,或者您有数字格式问题。要调试这种情况,你可以这样做(@realspirituals回答略有不同):

declare
  l_dummy number;
begin
  for cur in (select relation_id from policy_info)
  loop
     begin
        l_dummy := to_number(cur.relation_id);
     exception
        when others then dbms_output.put_line(cur.relation_id);
     end;
  end loop;
end;

这将打印来自policy_info.relation_id;

的所有违规值