2012-11-11 132 views
2

我需要更新字段'ErrCode'從2表中選擇。我嘗試查詢它的更新所有記錄。如何從選擇其他表更新?

表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 

表modifyTime

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 

我需要從表只更新LeaveEmp。

謝謝你,你的時間。 :)

回答

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' 
+0

感謝這麼淤泥它的工作! :d – nettoon493