带有WHERE EXISTS的我的UPDATE语句不限制SELECT语句结果

时间:2017-03-29 15:28:17

标签: sql ms-access jet

我在临时表中有数据,我正在检查另外两个表中的重复项。我想在临时表(SET DupFlag = TRUE)上为找到的所有重复项设置一个标志。我的SELECT语句工作正常,只返回我输入的48个重复项进行测试。但是当我添加UPDATE和WHERE EXISTS时,idTempImport2中的每个记录都设置为TRUE,而不仅仅是从SELECT语句返回的48条记录。我的语法错了吗?它看起来像这样:

UPDATE idTempImport2 as tmp2 SET DupFlag = TRUE
WHERE EXISTS
(SELECT * FROM idTempImport2 tmp2
LEFT JOIN (SELECT im.idDate, im.UserID, im.ActionID, im.IsHC, idn.Epic1, idn.Epic2 
      FROM idMain AS im 
      INNER JOIN idNotes AS idn ON im.aID = idn.MainID 
     WHERE idDate BETWEEN "2017-01-02" AND "2017-01-31")  AS qry 
ON qry.idDate = tmp2.idDate AND qry.UserID = tmp2.UserID AND qry.ActionID = tmp2.ActionID AND qry.Epic1 = clng(tmp2.Epic1) AND qry.Epic2 = clng(tmp2.Epic2)
WHERE qry.Epic1 <> NULL);

1 个答案:

答案 0 :(得分:1)

我认为最终的问题是你想要一个相关的子查询。如上所述,子查询与外部查询没有任何关联,因此可能至少有一行符合条件。所以,一切都会更新。

我想你打算:

UPDATE idTempImport2 as tmp2
    SET DupFlag = TRUE
WHERE EXISTS (SELECT im.idDate, im.UserID, im.ActionID, im.IsHC, idn.Epic1, idn.Epic2 
              FROM idMain AS im INNER JOIN
                   idNotes AS idn
                   ON im.aID = idn.MainID 
              WHERE idDate BETWEEN "2017-01-02" AND "2017-01-31" AND
                    im.idDate = tmp2.idDate AND im.UserID = tmp2.UserID AND
                    im.ActionID = tmp2.ActionID AND
                    ?.Epic1 = clng(tmp2.Epic1) AND ?.Epic2 = clng(tmp2.Epic2) 
             );