日期:2014-05-17  浏览次数:21136 次

写sql遇到了一个invalid number错误
语句select * from A,V where v.SWGLM = a.SWGLM 报invalid number
A是一个表,SWGLM是number(15)

B是一个视图
select E.SWGLM from E 
union all select F.SWGLM from F 
union all select to_char(G.SWGLM) from G
E.SWGLM是varchar2(32)
F.SWGLM是varchar2(32)
G.SWGLM是number(15)

我写成
select * from A,V where v.SWGLM = to_char(a.SWGLM)
select * from A,V where to_char(v.SWGLM) = to_char(a.SWGLM)
select * from A,V where to_number(v.SWGLM) = a.SWGLM
都没有用

------解决方案--------------------
V的SWGLM是什么
------解决方案--------------------
问题很简单,就是存在字符串型的数据,在转换为number时,未成功转换,也就是说这个字符串不是一个有效的数字字符串。

所以这个问题,无论怎么做转换都不能解决,只能查看数据,然后修改数据。
要不就都转换成字符串,应该是没有问题的。
------解决方案--------------------
是不是你的版本问题啊,测试了一把是正常的啊,pl/sql developer 10G 会自动转换的
------解决方案--------------------
a,v的字段类型呢?和B有什么关系?
------解决方案--------------------
v中有不能转成数字的字符!
SQL code

with t as(
     select 1001 id,'1000'num,'1002 ' name from dual
union all select 1002,'1001','1003 ' from dual
union all select 1003,'1002','1004 ' from dual
union all select 1004,'1003','1005 ' from dual
)
--select a.* from t a,t b where a.id=b.num;
select a.* from t a,t b where a.id=b.name;

------解决方案--------------------
写个方法把非数字型字符过滤即可!
SQL code
create   or   replace   function   isNumber(p_in   varchar2)   return   boolean   as   
        i   number;   
begin   
        i:=to_number(p_in);   
        return   true;   
exception         
        when   others   then     
              return   false;   
end   ;