下面的SQL在以下情況下正常工作:爲什麼SQL Server SELECT語句在SET語句內工作,但自行失敗?
USE ThisDB
UPDATE guest.TABLE2
SET this_var = (SELECT count(T1.key_field)
FROM ThisDB..TABLE1 T1
WHERE T1.key_field = guest.TABLE2.keyfield
AND T1.date_field between (DATEADD(DAY, -7, guest.TABLE2.other_date)) and guest.TABLE2.other_date)
然而,對自己,對SELECT
聲明(見下文)失敗,錯誤
多部分組成的標識符「guest.TABLE2。關鍵字段'不能被綁定。 (4104)
SELECT
count(T1.key_field)
FROM
ThisDB..TABLE1 T1
WHERE
T1.key_field = guest.TABLE2.keyfield
AND T1.date_field BETWEEN (DATEADD(DAY, -7, guest.TABLE2.other_date))
AND guest.TABLE2.other_date
因爲您未在您的select語句中加入guest.Table2。 –
這被稱爲* correlation *。內部查詢與外部查詢相關聯。 – shawnt00
您已選擇一個答案作爲正確答案,但未回答您的原始問題 –