是否有更好的 Oracle 运算符来进行空安全相等性检查?

2021-12-24 00:00:00 sql oracle plsql

根据这个问题,执行的方式Oracle 中的相等检查,我希望将 null 视为相等的 null 类似于

According to this question, the way to perform an equality check in Oracle, and I want null to be considered equal null is something like

SELECT  COUNT(1)
  FROM    TableA
WHERE 
  wrap_up_cd = val
  AND ((brn_brand_id = filter) OR (brn_brand_id IS NULL AND filter IS NULL))

这确实会使我的代码变脏,尤其是当我有很多这样的 where 并且 where 应用于多个列时.有没有更好的替代方案?

This can really make my code dirty, especially if I have a lot of where like this and the where is applied to several column. Is there a better alternative for this?

推荐答案

好吧,我不确定这是否更好,但使用 LNNVL,一个函数(你只能在 WHERE 子句中使用),如果给定的表达式为 FALSE 或 UNKNOWN (NULL),则返回 TRUE.例如...

Well, I'm not sure if this is better, but it might be slightly more concise to use LNNVL, a function (that you can only use in a WHERE clause) which returns TRUE if a given expression is FALSE or UNKNOWN (NULL). For example...

WITH T AS
(
    SELECT    1 AS X,    1 AS Y FROM DUAL UNION ALL
    SELECT    1 AS X,    2 AS Y FROM DUAL UNION ALL
    SELECT    1 AS X, NULL AS Y FROM DUAL UNION ALL
    SELECT NULL AS X,    1 AS Y FROM DUAL
)
SELECT
    *
FROM
    T
WHERE
    LNNVL(X <> Y);

...将返回除 X = 1 和 Y = 2 之外的所有行.

...will return all but the row where X = 1 and Y = 2.

相关文章