0

我的表结构是..

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 
             )  

当我尝试执行查询时,我得到了 Invalid Number 异常。如果您稍微修改查询 Like :

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

它执行得很好。

4

2 回答 2

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

这工作正常。请检查这个。如果您仍然遇到问题,请告诉我们完整的数据集

于 2013-09-26T08:57:43.667 回答
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 的所有违规值;

于 2013-09-26T09:37:44.857 回答