奇怪的更新行为
在我的数据仓库存储过程的一部分中,我有一个过程将旧项目数据与新项目数据进行比较(旧数据在表中,新数据在临时表中)并更新旧数据。
奇怪的是,如果旧数据为空,则更新语句不起作用。 如果我添加 is null 语句,则更新工作正常。 我的问题是,为什么这不像我想象的那样有效?
多个更新语句之一:
update cube.Projects
set prServiceLine=a.ServiceLine
from @projects1 a
inner join cube.Projects
on a.WPROJ_ID=cube.Projects.pk_prID
where prServiceLine<>a.ServiceLine
In a part of my data warehousing stored procedures, i have a procedure that compares old project data to new project data (old data is in a table, new in temp table) and updates the old data.
The weird part is that if the old data is null, then the update statement doesn't work. If i add a is null statement the update works fine. My question is, why doesn't this work like i thought it would?
One of the multiple update statements:
update cube.Projects
set prServiceLine=a.ServiceLine
from @projects1 a
inner join cube.Projects
on a.WPROJ_ID=cube.Projects.pk_prID
where prServiceLine<>a.ServiceLine
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果 prServiceLine 为 null 或 a.ServiceLine 为 null 则条件的结果为 null 不是布尔
测试:
输出:
您将永远不会看到“works”、“works!”、“not Yet”或“not Yet!” ' 得到输出。 只有“也许”(“也许!”,如果它们都相等)。
您不能使用 !=、= 或 <> 测试空值,如果您正在测试的值之一可以为 NULL,则需要在 WHERE 中使用 ISNULL()、COALESCE、IS NOT NULL 或 IS NULL 。
if prServiceLine is null or a.ServiceLine is null then the result of the condition is null not a boolean
test this:
output:
you will never see the 'works', 'works!', 'not yet', or 'not yet!' get output. Only the Maybe ('maybe!', if they were both equal).
you can't test null values with !=, =, or <>, you need to use ISNULL(), COALESCE, IS NOT NULL, or IS NULL in your WHERE if one of the values you are testing can be NULL.
关于 SQL Nulls 的维基百科文章实际上非常好 - 它解释了行为是如何进行的通常不是您所期望的,在许多情况下返回“未知”而不是 true 或 false。
如果引入空值,就会涉及到很多问题......
The Wikipedia article about SQL Nulls is actually really good - it explains how the behaviour is frequently not what you would expect, returning "unknown" rather than true or false in many instances.
There are quite a few gotchas involved if you introduce nulls...
也许您需要 LEFT JOIN(或 RIGHT JOIN,具体取决于数据)而不是 INNER JOIN。
Perhaps you need a LEFT JOIN (or RIGHT depending on data) rather than an INNER JOIN.