In a Zen-like manner, "NULL" is never equal to anything, even itself. Therefore comparisons using equality operators will always return
False
, even when the value actually IS NULL
.
For that reason, comparison operators should never be used to make comparisons with NULL
; IS NULL
and IS NOT
NULL
should be used instead. This extends as well to empty string (""), which is equivalent to NULL
for some database engines.
Noncompliant code example
SET SERVEROUTPUT ON
DECLARE
name VARCHAR2(42) := ''; /* This is equivalent to name := NULL; */
BEGIN
IF name = '' THEN /* Noncompliant; equivalent to name = NULL & therefore always FALSE*/
DBMS_OUTPUT.PUT_LINE('True'); /* Unreachable */
ELSE
DBMS_OUTPUT.PUT_LINE('False');
END IF;
END;
/
Compliant solution
SET SERVEROUTPUT ON
DECLARE
name VARCHAR2(42) := ''; /* This is equivalent to name := NULL; */
BEGIN
IF name IS NULL THEN /* Compliant */
DBMS_OUTPUT.PUT_LINE('True');
ELSE
DBMS_OUTPUT.PUT_LINE('False');
END IF;
END;
/