1

如果 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)
4

1 回答 1

1

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

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

于 2013-09-10T04:43:39.390 回答