首页 > 解决方案 > PLSQL - 当它们为NULL时查找两个值相等

问题描述

我正在使用 IF 条件来检查 2 个值是否相等。在一种情况下,我使用的所有值都是 NULL,但它不能识别它们是相等的。

IF training_event_rec_.training_fee = course_rec_.course_fee AND training_event_rec_.training_fee_unit = course_rec_.course_fee_unit AND training_event_rec_.currency = course_rec_.currency AND training_evaluation_temp_ = course_evaluation_temp_ THEN
    RETURN FALSE;
ELSE
     RETURN TRUE;
END IF;

在这里,所有比较的变量都是 NULL 但它总是命中 ELSE 部分。在这种情况下如何比较值。

标签: oracleplsqlnull

解决方案


使用 IS NULL:

IF     (training_event_rec_.training_fee = course_rec_.course_fee OR
       (training_event_rec_.training_fee IS NULL AND course_rec_.course_fee IS NULL))
   AND (training_event_rec_.training_fee_unit = course_rec_.course_fee_unit OR
        (training_event_rec_.training_fee_unit IS NULL AND course_rec_.course_fee_unit IS NULL))
   AND (training_event_rec_.currency = course_rec_.currency OR
        (training_event_rec_.currency IS NULL AND course_rec_.currency IS NULL))
   AND (training_evaluation_temp_ = course_evaluation_temp_ OR
        (training_evaluation_temp_ IS NULL AND course_evaluation_temp_ IS NULL))
THEN
    RETURN FALSE;
ELSE
     RETURN TRUE;
END IF;

笔记:

  • 仅当您确定 COLUMN_A 和 COLUMN_B 不包含值“X”时,您也可以比较 NVL(COLUMN_A,'X') = NVL(COLUMN_B,'X') 之类的 Null。所以这不是推荐的方法。

推荐阅读