Sql Server 不更新记录 [英] Sql Server not updating records

查看:41
本文介绍了Sql Server 不更新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个非常愚蠢的问题,但如果我运行以下查询,我无法理解为什么 sql server 不更新具有 NULL 值的记录:

I know this is very silly question but I am not able to understand why sql server is not updating records having NULL value, if I run below query:

UPDATE Students SET Resultsstatus = 'Final' where Resultsstatus != 'Hidden'  

我在某些情况下运行上述查询,我​​想将结果状态更新为最终";对于所有成绩状态不是隐藏"的学生.为了更新它们,我使用了上面的 sql 查询.但此查询仅适用于结果状态不是 NULL 的那些记录.

I run the above queries in certain circumstances where I want to update result status as "Final" for all those students who has not result status as "Hidden". To update them, I am using above sql queries. But this query is working only for those records who has result status other than NULL.

对于结果状态为 NULL 的记录,sql server 不会将这些记录更新为Final";使用上述查询.

For records where we have results status as NULL, sql server is not updating those records as "Final" using above query.

有人可以告诉我,上面的查询有什么问题吗?为什么不更新那些当前结果状态为 NULL 的记录?

Can someone please let me know, what is wrong with above query? Why it is not updating those records who has current result status as NULL?

提前致谢

推荐答案

因为 NULL 不等于也不等于任何东西,包括 NULL.1 != NULL 计算结果为 Unknown,not True;并且由于 Unknown 不是 True,则在 WHERE 中不返回该行.

Because NULL does not equal and does not not-equal anything, including NULL. 1 != NULL evaluates to Unknown, not True; and as Unknown is not True, then in a WHERE the row isn't returned.

在处理NULL值时,需要使用IS (NOT) NULL):

When dealing with NULL values, you need to use IS (NOT) NULL):

UPDATE dbo.Students
SET Resultsstatus = 'Final'
WHERE Resultsstatus != 'Hidden'  
   OR Resultsstatus IS NULL;

这篇关于Sql Server 不更新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆