我有两个 Oracle 12c (12.1.0.2.0) 数据库,其中一个对于以下查询(使用 SQL Developer 3.2.20.10)返回 'ok'
,而另一个则返回 ORA-01722:无效数字
:
select 'ok' from dual where 1 = nvl(1, 'X');
NVL
的 Oracle 文档说:
If expr1 is numeric, then Oracle determines which argument has the highest numeric precedence, implicitly converts the other argument to that datatype, and returns that datatype.
两个数据库之间的 NLS_COMP
、NLS_SORT
和 NLS_LANGUAGE
值相同,因此它们不应导致差异2 个参数的数字优先级。这两个数据库之间可能有什么不同,导致其中一个返回'ok'
而另一个返回错误?
最佳答案
cursor_sharing 可能是关键因素。
如果谓词“1 = nvl(1, 'X')”始终作为文字执行并优化为 true 或 false,则可以在解析时对其进行求值。但是,如果强制使用cursor_sharing,则所有三个文字都可以替换为其他值,并且在执行之前无法计算表达式。
我必须使用两个单独的本地表来测试它。
alter session set cursor_sharing=force;
create table me_dual as select * from dual;
select 'ok' from me_dual x where 1 = nvl(1, 'A');
select 'ok' from me_dual x where 1 = nvl(1, 'A')
ERROR at line 1:
ORA-01722: invalid number
*
alter session set cursor_sharing=exact;
create table alt_dual as select * from dual;
select 'ok' from alt_dual x where 1 = nvl(1, 'A');
'O
--
ok
关于sql - Oracle NVL 无效编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39455882/