sql - Oracle NVL 无效编号

标签 sql oracle

我有两个 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_COMPNLS_SORTNLS_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/

相关文章:

mysql - 查找每个客户的所有销售额

mysql - SQL like 和 concat 不返回值

.net - 如何使用 C# ODP.NET 中的 Oracle Ref Cursor 作为 ReturnValue 参数,而不使用存储函数或过程?

oracle - 删除标点符号

sql - 根据表数据查询表和列

sql - Oracle 11g - 如何优化慢速并行插入选择?

mysql - INSERT INTO COUNT(*) 在 MYSQL 中自动更新时间戳

sql - 带有部分单词的 MySQL 全文搜索

java - 在 JDBC 中使用时,查询返回的行数少于 SQL 开发人员

php - SQL ORDER BY 具有多个参数的情况