更新使用加入VS.在哪里存在

时间:2013-09-10 04:15:12

标签: sql join subquery where exists

如果JOIN和WHERE都在下面的代码中描述了内连接,那么子查询中的WHERE子句如何通过有选择地更新所需的行实际按预期工作,但子查询中的JOIN子句更新具有相同值的所有行。

UPDATE #TEMP_ST
SET Absence = 1
where exists (SELECT * FROM sd.studentlist sl join
              #TEMP_ST st ON st.id = sl.studentid and
              st.fiscalyear = sl.fiscalyear and
              st.schoolid = sl.schoolid and
              sl.absence = 1)


UPDATE #TEMP_ST
SET Absence = 1
where exists (SELECT * FROM sd.studentlist sl
              where #TEMP_ST.id = sl.studentid and
              #TEMP_ST.fiscalyear = sl.fiscalyear and
              #TEMP_ST.schoolid = sl.schoolid and
              sl.absence = 1)

1 个答案:

答案 0 :(得分:1)

在没有连接的子查询中,#TEMP_ST引用要更新的表。对于要更新的​​表中的每一行,使用要更新的行的值来评估子查询。

在具有连接的子查询中,您不引用要更新的表。无论要更新的行如何,子查询都会返回相同的结果。所有行都会受到影响,因为结果不为空。