首页 > 解决方案 > 查找数据行中的第一个空列

问题描述

我看到了类似的问题,大部分都变成了关于表设​​计、规范化等的争论。并不是我们所有人都有让我们的客户改变他们的数据库设计的奢侈。

这是我的困境。我的客户要求我让他们的员工能够增加前一天的呼叫时间。我将提供一个弹出窗口,允许他们输入前一天的 callout_start 和 callout_stop。许多人已经在前一天接到了电话。

CREATE TABLE MyTable(
empNo int NOT NULL,
workDate datetime NOT NULL,
callout_start1 datetime NULL,
callout_stop1 datetime NULL,
callout_start2 datetime NULL,
callout_stop2 datetime NULL,
callout_start3 datetime NULL,
callout_stop3 datetime NULL,
callout_start4 datetime NULL,
callout_stop4 datetime NULL,
callout_start5 datetime NULL,
callout_stop5 datetime NULL,
callout_start6 datetime NULL,
callout_stop6 datetime NULL,
callout_start7 datetime NULL,
callout_stop7 datetime NULL,
callout_start8 datetime NULL,
callout_stop8 datetime NULL,
callout_hours decimal(5, 2) NULL
)

我希望他们能够更新前一天的下一个可用标注。IOWs - 第一个为空的标注。SQL 必须是通用的。我正在写入一个 dbIsam 数据库,该数据库将通过 remObjects 同步到一个 MS/SQL DB。
谢谢

标签: sqldbisam

解决方案


好吧,您可以使用复杂的 SQL 语句来查找第一个null值。然后使用条件逻辑更新所有列:

with toupdate as (
      select t.*,
             (case when callout_start1 is null then 1
                   when callout_start2 is null then 2
                   . . .
              end) as which
      from t
     )
update toupdate
    set callout_start1 = (case when which = 1 then @param else callout_start1 end),
        callout_start2 = (case when which = 2 then @param else callout_start2 end),
        . . . 
    where <whatever>;
               

推荐阅读