ORA-01722 invalid number
ORA-01790: expression must have same datatype as corresponding expression
ORA-01847 day of month must be between 1 and last day of month
ORA-01858 a non-numeric character was found where a numeric was expected
ORA-01839 date not valid for month specified
ORA-01841 (full) year must be between -4713 and +9999, and not be 0
ORA-01843 not a valid month
select id
from (select id, data
from data_table
where data_type=‘housenum‘
)
where to_number(data) = 22;
他会变成如下等价的形式:
select id
from data_table
where data_type=‘housenum‘
and to_number(data) = 22;
9.2.0.7.0
SQL> create table data_table
(id number
,data_type varchar(12)
,data varchar(30)
);
Table created.
SQL> insert into data_table values (1234, ‘company‘,‘Pet Foods Inc‘);
1 row created.
SQL> insert into data_table values (1234, ‘contact‘,‘Jennifer‘);
1 row created.
SQL> insert into data_table values (1234, ‘zip‘,‘22‘);
1 row created.
SQL> insert into data_table values (1234, ‘shipdate‘,‘03-OCT-2003‘);
1 row created.
SQL> commit;
Commit complete.
SQL> select id
from data_table
where data_type=‘contact‘
and data=‘Jennifer‘;
ID
----------
1234
SQL> select id
from data_table
where data_type=‘zip‘
and to_number(data) = 22;
and to_number(data) = 22
*
ERROR at line 4:
ORA-01722: invalid number
会提示to_number的处理存在无效数字。
对其执行explain plan for,
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
1 - filter(TO_NUMBER("DATA_TABLE"."DATA")=22 AND
"DATA_TABLE"."DATA_TYPE"=‘zip‘)
SQL> select id
from (select id, data
from data_table
where data_type=‘zip‘
)
where to_number(data) = 22;
ID
----------
1234
这里的子查询仅会选择出data_type是ZIP的行,对应的data列值不包含非数字,因此可以正常执行。
对其执行explain plan for,
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
1 - filter("DATA_TABLE"."DATA_TYPE"=‘zip‘ AND
TO_NUMBER("DATA_TABLE"."DATA")=22)
先解析子查询,用data_type=‘zip‘过滤后,结果集的行data列都是数字型,因此to_number()可以正常执行。SQL> show parameter optimizer
NAME TYPE VALUE
------------------------------------ ---------------------- ------------------------------
optimizer_mode string CHOOSE
SQL> analyze table data_table compute statistics;
Table analyzed.
SQL> alter session set optimizer_mode=‘ALL_ROWS‘;
Session altered.
实验语句3:
SQL> select id
from data_table
where data_type=‘zip‘
and to_number(data) = 22
;
ID
----------
1234
这条语句执行explain plan for,
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
1 - filter("DATA_TABLE"."DATA_TYPE"=‘zip‘ AND
TO_NUMBER("DATA_TABLE"."DATA")=22)
SQL> select id
from (select id, data
from data_table
where data_type=‘zip‘
)
where to_number(data) = 22;
where to_number(data) = 22
*
ERROR at line 6:
ORA-01722: invalid number
但是现在第二条SQL执行报错。
这条语句执行explain plan for,发现谓词条件变为如下(顺序变了):
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
1 - filter(TO_NUMBER("DATA_TABLE"."DATA")=22 AND
"DATA_TABLE"."DATA_TYPE"=‘zip‘)
11.2.0.1.0
SQL> create table data_table
(id number
,data_type varchar(12)
,data varchar(30)
);
Table created.
SQL> insert into data_table values (1234, ‘company‘,‘Pet Foods Inc‘);
1 row created.
SQL> insert into data_table values (1234, ‘contact‘,‘Jennifer‘);
1 row created.
SQL> insert into data_table values (1234, ‘zip‘,‘22‘);
1 row created.
SQL> insert into data_table values (1234, ‘shipdate‘,‘03-OCT-2003‘);
1 row created.
SQL> commit;
Commit complete.
优化器模式是CBO:
SQL> show parameter optimizer
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
optimizer_mode string ALL_ROWS
SQL> select id
from data_table
where data_type=‘zip‘
and to_number(data) = 22;
ID
----------
1234
执行explain plan for,
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
1 - filter("DATA_TYPE"=‘zip‘ AND TO_NUMBER("DATA")=22)
SQL> select id from (select id, data from data_table where data_type=‘zip‘ ) where to_number(data) = 22; ID ---------- 1234 执行explain plan for, PLAN_TABLE_OUTPUT -------------------------------------------------------------------------------- 1 - filter("DATA_TYPE"=‘zip‘ AND TO_NUMBER("DATA")=22)
SQL> select id
2 from (select id, data
3 from data_table
4 where data_type=‘zip‘
5 )
6 where to_number(data) = 22;
where to_number(data) = 22
*
ERROR at line 6:
ORA-01722: invalid number
使用explain plan for后,
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
1 - filter(TO_NUMBER("DATA")=22 AND "DATA_TYPE"=‘zip‘)
版权声明:本文为博主原创文章,未经博主允许不得转载。
MOS文章实验:ORA-01722 from Queries with Dependent Predicates
原文地址:http://blog.csdn.net/bisal/article/details/47661241