【问题标题】:Copy rows that has atleast one null value in any column in SQL Server复制 SQL Server 中任何列中至少有一个空值的行
【发布时间】:2020-03-25 07:08:48
【问题描述】:

我有包含某些列的 table_1。我想将该数据复制到 table_2,在 table_1 的任何列中至少有一个空值。例如table_1 有三列。现在,我想将这些行复制到 table_2,在 table_1 的三列中的任何一列中至少有一个空值。

我使用以下查询进行了尝试:

insert into table_2 (col1, col2, col3)
select col1, col2, col3
from table_1
where col1 is null or col2 is null or col3 is null

但是,table_2 有一个列 'error_value' 存在一个问题,该列应包含指示哪些列具有对应于该特定行的 NULL 值的数据,就像它应该提到 'col2 is null' 如果 col2 在该行中缺少值。如果多于一列具有 NULL 值,那么如果所有列都有缺失值,则应提及 'error_value' 列中的所有列,例如 'col1, col2, col3' 为 null。

任何建议或帮助我如何实施它。

【问题讨论】:

  • 请提供示例数据+预期结果。
  • 所以col1col3 中的数据在源涂抹目标表之间应该是相同的,对吗?它只是一个过滤视图,其中包含缺失数据的记录子集,并包含一个总结缺失数据的新列。对吗?
  • 您能否提供一些示例,说明在存在 { 1, 2, 3 } 缺失值的情况下 error_value 消息应该是什么样的?
  • @JeremyCaney,当所有列都有缺失值时,它应该指示'col1, col2, col3 is null'。
  • 您使用的是什么版本的 SQL Server?

标签: sql sql-server tsql sql-server-2017


【解决方案1】:

使用CASE 表达式获取空列,然后连接。从 SQL Server 2017 开始,这可以通过 CONCAT_WS (https://docs.microsoft.com/de-de/sql/t-sql/functions/concat-ws-transact-sql?view=sql-server-ver15) 最好地实现。

insert into table_2 (col1, col2, col3, error_value)
select
  col1, col2, col3,
  concat_ws(', ',
    case when col1 is null then 'col1' end,
    case when col2 is null then 'col2' end,
    case when col3 is null then 'col3' end
  ) + ' is null'
from table_1
where col1 is null or col2 is null or col3 is null;

【讨论】:

  • 是的,对于 2017 年,这比我的回答更好。 +1。
【解决方案2】:

更新
2017 版本引入了concat_ws,它可以显着简化代码 - 查看Thorsten Kettner's answer for details。 我将这个答案留在这里,希望它对使用旧版本 SQL Server 的其他读者有所帮助。

第一版
一个简单的解决方案是使用caseconcatstuff 和字符串连接运算符 (+) 的组合,利用 concat 将隐式转换 null 为空的事实字符串,而+ 不会。

首先,创建并填充示例表(在您以后的问题中保存我们这一步):

create table table_1 (col1 int, col2 int, col3 int);
create table table_2 (col1 int, col2 int, col3 int, error_value varchar(100));

insert into table_1(col1, col2, col3) VALUES
(null, null, null),
(null, null, 1),
(null, 1, null),
(null, 1, 1),
(1, null, null),
(1, null, 1),
(1, 1, null),
(1, 1, 1);

然后,insert...select 声明:

insert into table_2 (col1, col2, col3, error_value)
select  
    col1, col2, col3, stuff(
        concat(
        ',' + case when col1 is null then 'col1' end, -- will be null if col1 contains a value
        ',' + case when col2 is null then 'col2' end, -- will be null if col2 contains a value
        ',' + case when col3 is null then 'col3' end, -- will be null if col3 contains a value
        ' is null'), 1, 1, '')
from table_1
where col1 is null or col2 is null or col3 is null

rextester上观看现场演示

【讨论】:

    【解决方案3】:

    如何使用 CONCAT 和 IIF 进行以下查询:

        insert into
        table_2 (col1, col2, col3, col4)
    select
        col1,
        col2,
        col3,
        CONCAT(
            IIF(col1 is null, 'col1 ', ''),
            IIF(col2 is null, 'col2 ', ''),
            IIF(col3 is null, 'col3 ', ''),
            ' is null'
        )
    from
        table_1
    where
        col1 is null
        or col2 is null
        or col3 is null
    

    【讨论】:

      【解决方案4】:

      尝试了简单的case语句和concat

      INSERT INTO #table_2 (col1, col2, col3,error_value)
      SELECT col1, col2, col3
      ,SUBSTRING(CONCAT(  CASE WHEN col1 IS NULL THEN ',col1' ELSE '' END
                          ,CASE WHEN col2 IS NULL THEN ',col2' ELSE '' END
                          ,CASE WHEN col3 IS NULL THEN ',col3' ELSE '' END
                      ),2,20) + ' is null'
      FROM #table_1
      WHERE col1 IS NULL OR col2 IS NULL OR col3 IS NULL
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-13
        • 2021-09-13
        • 2021-05-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多