2

我需要从 2 个表之间的选择中更新字段“ErrCode”。我尝试查询它更新所有记录。

表 LeaveEmp

EmpRun Date                      Start                    Stop
00003  2012-10-02 00:00:00.000   2012-10-02 13:00:00.000  2012-10-02 17:00:00.000
00004  2012-10-02 00:00:00.000   NULL                     NULL

表修改时间

EmpNo  cDate                     cIn                      cOut                      ErrCode
00001  2012-10-02 00:00:00.000   NULL                     NULL                      NULL
00002  2012-10-02 00:00:00.000   2012-10-02 07:35:00.000  2012-10-02 20:12:00.000   NULL
00003  2012-10-02 00:00:00.000   2012-10-02 13:00:00.000  2012-10-02 17:00:00.000   NULL 
00004  2012-10-02 00:00:00.000   NULL                     NULL                      NULL
00005  2012-10-02 00:00:00.000   2012-10-02 07:13:00.000  2012-10-02 19:24:00.000   NULL
00006  2012-10-02 00:00:00.000   2012-10-02 07:55:00.000  2012-10-02 20:58:00.000   NULL
*****00003,00004 from Table LeaveEmp.step one i insert Table LeaveEmp to Table modifyTime**

代码:

UPDATE modifyTime
SET ErrCode = '3'
WHERE EXISTS (SELECT coalesce(l.Start,'2012-10-02')
FROM LeaveEmp l
WHERE l.Start = '2012-10-02' and modifyTime.cDate = '2012-10-02')

这得到输出:

EmpNo  cDate                     cIn                      cOut                      ErrCode
00001  2012-10-02 00:00:00.000   NULL                     NULL                      3
00002  2012-10-02 00:00:00.000   2012-10-02 07:35:00.000  2012-10-02 20:12:00.000   3
00003  2012-10-02 00:00:00.000   2012-10-02 13:00:00.000  2012-10-02 17:00:00.000   3 
00004  2012-10-02 00:00:00.000   NULL                     NULL                      3
00005  2012-10-02 00:00:00.000   2012-10-02 07:13:00.000  2012-10-02 19:24:00.000   3
00006  2012-10-02 00:00:00.000   2012-10-02 07:55:00.000  2012-10-02 20:58:00.000   3

但我需要输出:

EmpNo  cDate                     cIn                      cOut                      ErrCode
00001  2012-10-02 00:00:00.000   NULL                     NULL                      NULL
00002  2012-10-02 00:00:00.000   2012-10-02 07:35:00.000  2012-10-02 20:12:00.000   NULL
00003  2012-10-02 00:00:00.000   2012-10-02 13:00:00.000  2012-10-02 17:00:00.000   3 
00004  2012-10-02 00:00:00.000   NULL                     NULL                      3
00005  2012-10-02 00:00:00.000   2012-10-02 07:13:00.000  2012-10-02 19:24:00.000   NULL
00006  2012-10-02 00:00:00.000   2012-10-02 07:55:00.000  2012-10-02 20:58:00.000   NULL

我只需要从 Table LeaveEmp 更新。

谢谢你的时间。:)

4

1 回答 1

1

您缺少 modifyTime 和 LeaveEmp 之间的链接...试试这个

UPDATE modifyTime
SET ErrCode = '3'
FROM modifyTime m
INNER JOIN LeaveEmp l
  ON m.EmpNo = l.EmpRun
WHERE l.Start = '2012-10-02' and modifyTime.cDate = '2012-10-02'
于 2012-11-11T16:55:20.957 回答