“无法将 NULL 值插入列”当尝试更新 SQL 中的列时

发布于 2024-11-18 18:16:52 字数 912 浏览 1 评论 0原文

我正在尝试使用下面的查询更新不为空的列的值

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

关于从前 2024-11-25 18:16:52

在语句末尾添加 ELSE 子句以捕获所有条件都不匹配的情况。这应该消除错误。所以像这样:

...
  when CurrencyId='SGD' then 'SG'
  when CurrencyId='TWD' then 'TW'
  when CurrencyId='ZAR' then 'ZA'
  else '??'
end

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:

...
  when CurrencyId='SGD' then 'SG'
  when CurrencyId='TWD' then 'TW'
  when CurrencyId='ZAR' then 'ZA'
  else '??'
end
水染的天色ゝ 2024-11-25 18:16:52

这是一个相当令人讨厌的案例陈述。我的猜测是您与您的案例之一不匹配。

想想看,用不同的方式实现同​​样的事情不是更容易吗?

除了少数例外,几乎所有更新都可以使用子字符串完成。

例如

UPDATE
  BOM
SET 
  CountryCode = SUBSTRING(CountryCode,1,2)
WHERE
  CountryCode IN
(
  'CHF',
  'HKD'
  -- the rest of these go here
)

然后手动执行异常。

虽然你已经找到了这个具体问题的答案,这很酷,但我的建议是发现其中的模式,不要让自己的生活变得太艰难。

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.

UPDATE
  BOM
SET 
  CountryCode = SUBSTRING(CountryCode,1,2)
WHERE
  CountryCode IN
(
  'CHF',
  'HKD'
  -- the rest of these go here
)

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.

白首有我共你 2024-11-25 18:16:52

首先,您的案例陈述可以简化。

 CASE CurrencyId 
 WHEN 'CHF' THEN 'CH'
 WHEN 'NZD' THEN 'NZ' 
 WHEN 'KRW' THEN 'KR' 
 END

其次,必须有一个条件与其中任何一个都不匹配。进行选择以找到它:

        SELECT *
        FROM Bom
        WHERE case CurrencyId 
        when 'CHF' then 'CH' 
        when 'NZD' then 'NZ' 
        when 'KRW' then 'KR' 
        ...
        end IS NULL

First, your case statement can be simplified.

 CASE CurrencyId 
 WHEN 'CHF' THEN 'CH'
 WHEN 'NZD' THEN 'NZ' 
 WHEN 'KRW' THEN 'KR' 
 END

Second, there must be a condition that doesn't match any of them. Do a select to find it:

        SELECT *
        FROM Bom
        WHERE case CurrencyId 
        when 'CHF' then 'CH' 
        when 'NZD' then 'NZ' 
        when 'KRW' then 'KR' 
        ...
        end IS NULL
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文