【问题标题】:Cannot insert NULL value in column error in SQL Server 2017无法在 SQL Server 2017 的列错误中插入 NULL 值
【发布时间】:2020-12-19 15:16:05
【问题描述】:

这是我的查询

insert into membre (code) 
select code = case when trig = '' then 'toto' else case when trig is null 'titi' end end
from test

我想将此结果插入表测试,但会打印出此错误 .请你掌舵我好吗?

【问题讨论】:

  • 要么使表列可以为空,要么首先防止空值到达那里。
  • 我无法更改表测试
  • 您需要满足所有情况或在您的情况下使用else 块。如果 trig 既不是空字符串也不是 null 怎么办?您也可以将内壳与外壳组合在一起。

标签: sql tsql case sql-insert


【解决方案1】:

我怀疑你想要以下逻辑

insert into membre(code)
select case 
    when trig = '' then 'toto'
    when trig is null then 'titi'
    else trig
end
from test

这会将test(trig) 插入到membre(code),同时将空字符串转换为'toto'null 值到'titi'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多