“无法将 NULL 值插入列”当尝试更新 SQL 中的列时
我正在尝试使用下面的查询更新不为空的列的值
update Bom set CountryCode=
case
when CurrencyId='CHF' then 'CH'
when CurrencyId='NZD' then 'NZ'
when CurrencyId='KRW' then 'KR'
when CurrencyId='HKD' then 'HK'
when CurrencyId='MXN' then 'MX'
when CurrencyId='USDe' then 'DE'
when CurrencyId='JPY' then 'JP'
when CurrencyId='EUR' then 'DE'
when CurrencyId='DKK' then 'DK'
when CurrencyId='GBP' then 'GB'
when CurrencyId='AUD' then 'AU'
when CurrencyId='RMB' then 'CN'
when CurrencyId='USDu' then 'US'
when CurrencyId='NOK' then 'NO'
when CurrencyId='CAD' then 'CA'
when CurrencyId='USDm' then 'MX'
when CurrencyId='SEK' then 'SE'
when CurrencyId='SGD' then 'SG'
when CurrencyId='TWD' then 'TW'
when CurrencyId='ZAR' then 'ZA'
end
,但由于某种原因,我收到一条错误消息
无法将 NULL 值插入 列“国家代码”,表 'Mouser_BOM.dbo.Bom';列没有 允许空值。更新失败。
我没有插入任何空值,但仍然收到此错误。有人可以帮助我为什么会得到这个吗?
I am trying to update values of a column which is not null using the query below
update Bom set CountryCode=
case
when CurrencyId='CHF' then 'CH'
when CurrencyId='NZD' then 'NZ'
when CurrencyId='KRW' then 'KR'
when CurrencyId='HKD' then 'HK'
when CurrencyId='MXN' then 'MX'
when CurrencyId='USDe' then 'DE'
when CurrencyId='JPY' then 'JP'
when CurrencyId='EUR' then 'DE'
when CurrencyId='DKK' then 'DK'
when CurrencyId='GBP' then 'GB'
when CurrencyId='AUD' then 'AU'
when CurrencyId='RMB' then 'CN'
when CurrencyId='USDu' then 'US'
when CurrencyId='NOK' then 'NO'
when CurrencyId='CAD' then 'CA'
when CurrencyId='USDm' then 'MX'
when CurrencyId='SEK' then 'SE'
when CurrencyId='SGD' then 'SG'
when CurrencyId='TWD' then 'TW'
when CurrencyId='ZAR' then 'ZA'
end
but for some reason I am getting an error saying
Cannot insert the value NULL into
column 'CountryCode', table
'Mouser_BOM.dbo.Bom'; column does not
allow nulls. UPDATE fails.
I am not inserting any null values but still getting this error. Can someone help why I am getting this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在语句末尾添加 ELSE 子句以捕获所有条件都不匹配的情况。这应该消除错误。所以像这样:
Add an ELSE clause to the end of your statement to catch the case that none of your conditions match. That should get rid of the error. So something like:
这是一个相当令人讨厌的案例陈述。我的猜测是您与您的案例之一不匹配。
想想看,用不同的方式实现同样的事情不是更容易吗?
除了少数例外,几乎所有更新都可以使用子字符串完成。
例如
然后手动执行异常。
虽然你已经找到了这个具体问题的答案,这很酷,但我的建议是发现其中的模式,不要让自己的生活变得太艰难。
That's a rather nasty case statement there. My guess is that you're not matching one of your cases.
Looking at it, isn't it easier to achieve the same thing in a different way?
With a few exceptions, pretty much all of your updates could be done with a substring.
e.g.
Then do the exceptions manually.
While it's cool that you've got an answer to this specific problem, my advice is to spot the patterns and not make life too hard on yourself.
首先,您的案例陈述可以简化。
其次,必须有一个条件与其中任何一个都不匹配。进行选择以找到它:
First, your case statement can be simplified.
Second, there must be a condition that doesn't match any of them. Do a select to find it: