Oracle Differences between NVL and Coalesce(NVL 和 Coalesce 之间的 Oracle 差异)
问题描述
在 Oracle 中 NVL 和 Coalesce 之间是否有不明显的区别?
Are there non obvious differences between NVL and Coalesce in Oracle?
明显的区别是,coalesce 将返回其参数列表中的第一个非空项,而 nvl 只接受两个参数,如果不为空则返回第一个,否则返回第二个.
The obvious differences are that coalesce will return the first non null item in its parameter list whereas nvl only takes two parameters and returns the first if it is not null, otherwise it returns the second.
似乎 NVL 可能只是合并的基本案例"版本.
It seems that NVL may just be a 'Base Case" version of coalesce.
我错过了什么吗?
推荐答案
COALESCE 是更现代的函数,是 ANSI-92 标准的一部分.
COALESCE is more modern function that is a part of ANSI-92 standard.
NVL 是 Oracle 特有的,它在 80's 在没有任何标准之前被引入.
NVL is Oracle specific, it was introduced in 80's before there were any standards.
如果有两个值,它们是同义词.
In case of two values, they are synonyms.
但是,它们的实现方式不同.
However, they are implemented differently.
NVL 总是对两个参数求值,而 COALESCE 通常在找到第一个非 NULL 时停止求值(有一些例外,例如作为序列 NEXTVAL):
NVL always evaluates both arguments, while COALESCE usually stops evaluation whenever it finds the first non-NULL (there are some exceptions, such as sequence NEXTVAL):
SELECT SUM(val)
FROM (
SELECT NVL(1, LENGTH(RAWTOHEX(SYS_GUID()))) AS val
FROM dual
CONNECT BY
level <= 10000
)
这运行了几乎 0.5 秒,因为它生成了 SYS_GUID(),尽管 1 不是 NULL代码>.
This runs for almost 0.5 seconds, since it generates SYS_GUID()'s, despite 1 being not a NULL.
SELECT SUM(val)
FROM (
SELECT COALESCE(1, LENGTH(RAWTOHEX(SYS_GUID()))) AS val
FROM dual
CONNECT BY
level <= 10000
)
这理解 1 不是 NULL 并且不评估第二个参数.
This understands that 1 is not a NULL and does not evaluate the second argument.
SYS_GUID 不会生成并且查询是即时的.
SYS_GUID's are not generated and the query is instant.
这篇关于NVL 和 Coalesce 之间的 Oracle 差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:NVL 和 Coalesce 之间的 Oracle 差异
基础教程推荐
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 无法解决整理冲突 2021-01-01
