ORACLE的LNNVL函數使用

LNNVL官方解釋

LNNVL provides a concise way to evaluate a condition when one or both operands of the condition may be null. The function can be used only in the WHERE clause of a query. It takes as an argument a condition and returns TRUE if the condition is FALSE or UNKNOWN and FALSE if the condition is TRUE. LNNVL can be used anywhere a scalar expression can appear, even in contexts where the IS [NOT] NULL, AND, or OR conditions are not valid but would otherwise be required to account for potential nulls. Oracle Database sometimes uses the LNNVL function internally in this way to rewrite NOT IN conditions as NOT EXISTS conditions. In such cases, output from EXPLAIN PLAN shows this operation in the plan table output. The condition can evaluate any scalar values but cannot be a compound condition containing AND, OR, or BETWEEN.


LNNVL官方解釋翻譯
lnnvl用於某個語句的where子句中的條件,如果條件爲true就返回false;如果條件爲UNKNOWN或者false就返回true。該函數不能用於複合條件如AND, OR, or BETWEEN中。

模擬測試環境

SQL> create table xifenfei(name varchar2(20),year number);
 
Table created.
 
SQL> insert into xifenfei values('xifenfei2001',2001);
 
1 row created.
 
SQL> insert into xifenfei values('xifenfei2002',2002);
 
1 row created.
 
SQL> insert into xifenfei values('xifenfei2003',2003);
 
1 row created.
 
SQL> insert into xifenfei values('xifenfei2004',2004);
 
1 row created.
 
SQL> insert into xifenfei values('xifenfei2005',2005);
 
1 row created.
 
SQL> insert into xifenfei values('xifenfei2006',2006);
 
1 row created.
 
SQL> insert into xifenfei values('xifenfei2007',2007);
 
1 row created.
 
SQL> insert into xifenfei values('xifenfei2008',null);
 
1 row created.
 
SQL> insert into xifenfei values('xifenfei2009',2009);
 
1 row created.
 
SQL> insert into xifenfei values('xifenfei2010',2010);
 
1 row created.
 
SQL> insert into xifenfei values('xifenfei2011',2011);
 
1 row created.
 
SQL> commit;
 
Commit complete.
 
SQL> select * from xifenfei;
 
NAME                       YEAR
-------------------- ----------
xifenfei2001               2001
xifenfei2002               2002
xifenfei2003               2003
xifenfei2004               2004
xifenfei2005               2005
xifenfei2006               2006
xifenfei2007               2007
xifenfei2008
xifenfei2009               2009
xifenfei2010               2010
xifenfei2011               2011
 
11 rows selected.

幾種情況測試說明

--年份小於2009(lnnvl表示年份大於或者2009包含null)
SQL> select * from xifenfei where lnnvl(year<2009);
 
NAME                       YEAR
-------------------- ----------
xifenfei2008
xifenfei2009               2009
xifenfei2010               2010
xifenfei2011               2011
 
--year不爲null(lnnvl表示年份爲null)
SQL> select * from xifenfei where lnnvl(year is not null);
 
NAME                       YEAR
-------------------- ----------
xifenfei2008
 
--年份爲null(lnnvl表示年份不爲null)
SQL> select * from xifenfei where lnnvl(year is  null);
 
NAME                       YEAR
-------------------- ----------
xifenfei2001               2001
xifenfei2002               2002
xifenfei2003               2003
xifenfei2004               2004
xifenfei2005               2005
xifenfei2006               2006
xifenfei2007               2007
xifenfei2009               2009
xifenfei2010               2010
xifenfei2011               2011
 
10 rows selected.
 
--年份爲12345(lnnvl表示年份不爲12345)
SQL> select * from xifenfei where lnnvl(year =12345);
 
NAME                       YEAR
-------------------- ----------
xifenfei2001               2001
xifenfei2002               2002
xifenfei2003               2003
xifenfei2004               2004
xifenfei2005               2005
xifenfei2006               2006
xifenfei2007               2007
xifenfei2008
xifenfei2009               2009
xifenfei2010               2010
xifenfei2011               2011
 
11 rows selected.
 
--年份不爲12345(lnnvl表示年份爲12345或者null)
SQL> select * from xifenfei where lnnvl(year !=12345);
 
NAME                       YEAR
-------------------- ----------
xifenfei2008

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章