oracle - 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 部分。在这种情况下如何比较值。
解决方案
使用 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。所以这不是推荐的方法。
推荐阅读
- node.js - 在打字稿中导入哈希时出现异常
- gradle - 使用复合 gradle 构建时出现异常
- php - 如何让 php 显示 cookie 的值?
- ios - 为什么在 iOS 上运行的空白离子电容器应用程序返回此错误:无法连接到服务器
- security - 保护 hashcorp 保险库 API 令牌
- c++ - 如何以矩阵形式打印我的 2D 矢量?C++
- java - 为什么我的代码说 JOptionPane 和 Interger 是无法找到的变量?
- python - Python中两组不同数据的合并方法
- python - 在 jinja 中引用渲染函数参数
- c# - C#使用`[]`保存具有特定文件名的excel文件不起作用