oracle 短路与非短路函数(一)

来源:这里教程网 时间:2026-03-03 15:43:38 作者:

tip: 短路运算减少执行语句,优化了性能! 运算符里:AND和OR都有短路计算功能,常见函数里又是怎么样的呢? 一:非短路函数    1:NVL   2: NVL2   二:短路函数   1: decode   2: case when   3: coalesce 三:实验

SQL> select * from v$version;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE    11.2.0.1.0      Production
TNS for 64-bit Windows: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
--NVL(1,0,1) = 2 被短路
SQL> select * from dual where 1 = 1 or nvl(1/0,2) = 2;
DU
--
X
--NVL(1,0,1) = 2 被短路
SQL>   select * from dual where 1 = 2 and nvl(1/0,1) = 2;
no rows selected
--1/0 继续运算,非短路
SQL> select nvl(1,1/0) from dual;
	select nvl(1,1/0) from dual
				  *
	ERROR at line 1:
	ORA-01476: divisor is equal to zero
--1/0 继续运算,非短路
SQL> select nvl2(null,1/0,2) from dual;
select nvl2(null,1/0,2) from dual
				  *
ERROR at line 1:
ORA-01476: divisor is equal to zero
--1/0 没有运算,短路
SQL> select decode(1,1,2,1/0) from dual;
DECODE(1,1,2,1/0)
-----------------
				2
				
--1/0 没有运算,短路
SQL> select coalesce(null,1,1/0) from dual;
COALESCE(NULL,1,1/0)
--------------------
				   1
--1/0 没有运算,短路
SQL> select case when 1 is not null then 1 else 1/0 end col from dual;
COL
----------
1

相关推荐