真正的空安全比较

时间:2018-11-06 17:00:01

标签: sql-server tsql

是否有一种真正安全的方法来检查短于此长度的T-SQL中两个可为空的值不相等?

where
    A.MyField != B.MyField
or  (
        A.MyField is null
    and B.MyField is not null
)
or  (
        A.MyField is not null
    and B.MyField is null
)

使用isnull()并不是真正安全,因为它将null值折叠为数据集中可能存在的“真实”值,例如:

where
    isnull(A.MyField, '') != isnull(B.MyField, '')

会错误地认为空字符串''null相等,这不是期望的结果。您可能会想出一个永远不会发生或极不可能发生的“已知”值,但这似乎是一个创可贴。

出于某些原因(尤其是该功能已被弃用),也不希望关闭ANSI_NULLS

是否存在可以进行“真实”的空安全检查的功能,还是上面的代码是最好的方法?

1 个答案:

答案 0 :(得分:1)

You can use

Traceback (most recent call last):
  File "C:\x\g_4.py", line 147, in <module>
    app = SeaofBTCapp()
  File "C:\x\g_4.py", line 40, in __init__
    frame = F(container, self)
  File "C:\x\g_4.py", line 141, in __init__
    wordde.grid(row=3, column=1)
NameError: name 'wordde' is not defined