在 SQL Server 2008R2 中改写 IS DISTINCT FROM 和 IS NOT DISTINCT FROM
问题:
在 Microsoft SQL Server 2008R2(不支持 IS DISTINCT FROM 和 IS NOT DISTINCT FROM 运算符)中,如何改写包含 IS DISTINCT FROM 和 IS NOT DISTINCT FROM 运算符的表达式?
解答:
IS DISTINCT FROM
IS DISTINCT FROM 谓词在比较两个值时保证二元结果(真或假)。对于 SQL Server 2008R2,可以使用以下表达式模拟此谓词:
<code class="language-sql">((a != b OR a IS NULL OR b IS NULL) AND NOT (a IS NULL AND b IS NULL))</code>
IS NOT DISTINCT FROM
类似地,IS NOT DISTINCT FROM 检查两个值之间是否存在明显的差异。它可以改写为:
<code class="language-sql">(NOT (a != b OR a IS NULL OR b IS NULL) OR (a IS NULL AND b IS NULL))</code>
与错误答案的比较
给出的错误答案未能考虑布尔表达式中 NULL 值的计算。例如,NULL IS DISTINCT FROM NULL 应计算为 False,但错误表达式会产生 Unknown。
以上是如何在SQL Server 2008R2中重写IS DISTINCT FROM和IS NOT DISTINCT FROM?的详细内容。更多信息请关注PHP中文网其他相关文章!