溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Oracle nvl、nvl2、nullif、decode、case函數詳解

發布時間:2020-07-16 00:24:45 來源:網絡 閱讀:1101 作者:oracle夢想 欄目:關系型數據庫

1、NVL函數

    nvl(expr1,expr2),如果expr1為空,則返回expr2;


2、NVL2函數

    nvl2(expr1,expr2,expr3),如果expr1為空,則返回expr3,否則返回expr2;


3、NULLIF函數

    nullif(expr1,expr2),如果expr1=expr2,返回空,否則返回expr1,要求兩個表達式數據類型一致;


    SQL> insert into t1 values(9);

說明1:NVL和NVL2函數在進行空值判斷的時候,都會將函數內的表達式執行一次。


4、DECODE函數:

     是oracle數據庫獨家提供的函數功能,不是sql標準,

     相當于程序語言中的 if 1=1 then 1 else 1!=1的執行效果;

      DECODE(value, if1, then1, if2,then2, if3,then3, . . . else )

5、CASE函數

            case expr 

                        when comparison_expr1 then return_expr1

            when comparison_expr2 then return_expr2

            when comparison_expr3 then return_expr3

                        ......

                        when comparison_exprN then return_exprN

             end

關于nvl、nvl2、decode函數執行性能比較

    SQL> create table t1 (i int);--創建t1臨時表

    SQL> insert into t1 values(9);

    SQL> insert into t1 values(9);--插入3行數據,數據值都是9

    SQL> create or replace function sleep_now return number is

      2  i number;

      3  begin

      4  i :=0;

      5  while8

      6  i<=1000000

      7  loop

      8  i :=i+1;

      9  end loop;

     10  return i;

     11  end;

     12  /                         --創建一個sleep_now函數,目的是為了加長sql執行的時間

    SQL> set timing on;            --設置sqlplus命令執行時間

    SQL> select  nvl(i,sleep_now()) from t1;

     

    NVL(I,SLEEP_NOW())

    ------------------

                     9

                     9

                     9

     

    Executed in 0.343 seconds  --判斷t1表中的i字段是否為空,為空則執行sleep_now()函數;

    sql執行時間是0.343秒;

    SQL> select  nvl2(i,sleep_now(),1) from t1;

     

    NVL2(I,SLEEP_NOW(),1)

    ---------------------

                  1000001

                  1000001

                  1000001

     

    Executed in 0.343 seconds  --同樣使用nvl2函數進行測試


    --使用decode進行相同測試,執行時間是0.063秒

    SQL> select decode(i,null,sleep_now(),1111) from t1;

     

    DECODE(I,NULL,SLEEP_NOW(),1111

    ------------------------------

                              1111

                              1111

                              1111

     

    Executed in 0.063 seconds

總結:錯誤的、不恰當的使用nvl函數,后患無窮!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女