共計 1339 個字符,預計需要花費 4 分鐘才能閱讀完成。
自動寫代碼機器人,免費開通
本篇文章為大家展示了 oracle 中怎么實現短路與非短路函數,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
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;
--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)
-----------------
--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;
----------
1
上述內容就是 oracle 中怎么實現短路與非短路函數,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注丸趣 TV 行業資訊頻道。
向 AI 問一下細節
正文完
發表至: 數據庫
2023-12-04