首页 > 解决方案 > 当我们使用加法赋值 (+=) 时更新空值

问题描述

declare @temp table
(
    Id int,
    Qty int 
)

insert into @temp values(1,null)
update @temp set Qty+=2

select * from @temp

我正在尝试更新该Qty列,但该Qty列默认为空。因此,因为当我尝试更新时生产数据库列可能为空,所以我得到一个空值,我需要Qty为 2。

标签: sqlsql-servertsqlsql-server-2012

解决方案


使用coalesce(or isnull) 来处理 null 情况。

update @temp set Qty = coalesce(Qty,0) + 2;

推荐阅读