加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

SQLServer更新语句对空值扼流,即使使用isNull()

发布时间:2020-12-12 14:17:47 所属栏目:MsSql教程 来源:网络整理
导读:请注意,我已经更改了表格和字段的名称,以使其简短易懂. 我有一个问题,归结为,来到这个: update destTable set destField = ( select top 1 isnull(s.sourceField,'') from sourceTable s where various matches between the destTable table and the s tabl
请注意,我已经更改了表格和字段的名称,以使其简短易懂.

我有一个问题,归结为,来到这个:

update destTable 
set destField = ( select top 1 isnull(s.sourceField,'') from sourceTable s 
where <various matches between the destTable table and the s table>
);

(我知道语法’更新destTable设置destField …来自destTable d,sourceTable s …’,但我不知道如何将“前1”放入其中.)

从这里我得到SQLServer 2012 Express结果:

Msg 515,Level 16,State 2,Line 1
Cannot insert the value NULL into column 'destField',table 'destTable'; column does not allow nulls. UPDATE fails.
The statement has been terminated.

对于这两个表,所有字段都定义为非null和default(”).

“前1”很重要,因为sourceTable可能有多个匹配“where”子句.

我查询了sourceTable的所有行,发现它的所有sourceField值都是非null.然而,我得到了结果.

查询的本质是,在1000个destTable记录中,与sourceTable的匹配将仅产生300行的匹配.其他700个destTable记录将没有匹配项.

我不明白SQLServer对我做了什么.当我上次使用MySQL运行它时,此查询工作正常.

提前致谢,
杰罗姆.

解决方法

问题是查询没有返回任何行. . .因此产生一个NULL值.将isNULL()移到子查询外:

update destTable 
set destField =  isnull((select top 1 s.sourceField
                         from sourceTable s 
                         where <various matches between the destTable table and the s table>
                        ),'');

顺便说一句,通常我会在这种情况下提倡coalesce(),因为它是标准.然而,正如Aaron Bertrand here所描述的那样,两者表现不同.特别是,第一个参数似乎被评估两次,当它是一个子查询时非常昂贵. Isnull()显然没有这个问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读