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

Is there better Oracle operator to do null-safe equality check?(是否有更好的 Oracle 运算符来进行空安全相等性检查?)
本文介绍了是否有更好的 Oracle 运算符来进行空安全相等性检查?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据这个问题,执行的方式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.

这篇关于是否有更好的 Oracle 运算符来进行空安全相等性检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)