ORA-01722: invalid number 异常【我改】

我的情况是,在sql中两个字段相等最为条件时,这两个字段一个类型是字符串,一个是数字,而字符串类型的字段中有一条记录不能转成为数字造成的,解决方法,用 to_char将数字类型的字段转换为字符串再比较。

转:

ORA-01722: invalid number

如果某一个列定义的是varchar2字符串类型的,查询的时候使用了where xxx=1,让列名等于一个数字,那么,如果这个列里面都是数字,那么不报错,如果列里面只要有一个是非数字的,则报错。
因为,oracle使用了隐式的转换,to_number(xxx)=1,如果xxx列里面有不能转换为数字的,则报错。


下面进行验证
SQL> create table gw1(name varchar2(5));
Table created.
SQL> insert into gw1 values(1);
1 row created.
SQL> commit;
Commit complete.
SQL> select * from gw1 where name=1;
NAME
----------
1
SQL> truncate table gw1;
Table truncated.
SQL> insert into gw1 values('a');
1 row created.
SQL> commit;
Commit complete.
SQL> select * from gw1 where name=1;
select * from gw1 where name=1
                        *
ERROR at line 1:
ORA-01722: invalid number
SQL> insert into gw1 values(1);  
1 row created.
SQL> insert into gw1 values(2);
1 row created.
SQL> commit;
Commit complete.
SQL> select * from gw1 where name=1;
select * from gw1 where name=1
                        *
ERROR at line 1:
ORA-01722: invalid number


SQL> explain plan for select * from gw1 where name=1;
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 3401378285
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     1 |     4 |     3   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| GW1  |     1 |     4 |     3   (0)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter(TO_NUMBER("NAME")=1)----------------------------


然后检查两个用户的两个表,发现不报错的用户的表里面的字段都是数字,而报错的用户表里面的字段有字母。
————————————————
版权声明:本文为CSDN博主「娜然」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/nanaranran/article/details/79670873

原文地址:https://www.cnblogs.com/libin6505/p/12092862.html