2010-10-05 91 views
19

任何人都可以在此查詢中找到我的錯誤嗎?我正在使用SQL Server 2000,我想將CostEntry表中的所有條目更新爲ActiveCostDetails表中的相應值。 where子句確實與select語句一起工作。使用內部連接的SQL更新查詢語法

UPDATE CostEntry CE 
INNER JOIN ActiveCostDetails As AD ON CostEntry.lUniqueID = ActiveCostDetails.UniqueID 
     SET CostEntry.sJobNumber = ActiveCostDetails.JobNumber 
    WHERE CostEntry.SEmployeeCode = '002' 
     AND SubString(CostCentre, 1, 1) = sDepartmentCode 
     AND substring(CostCentre, 3, 1) = sCategoryCode 
     AND substring(CostCentre, 5, 2) = sOperationCode 
+0

@OMG Ponies-感謝您尋找,但此查詢具有與原來相同的效果(由於不工作,語法錯誤)。我相信這是因爲SQL服務器忽略查詢中的空格和回車符。 – MAW74656 2010-10-05 19:54:08

+0

我剛剛重新格式化了你發佈的內容 - 更容易閱讀,更容易幫助 – 2010-10-05 19:55:52

+0

好的小馬,我明白了。我正在做一個例子,在線... http://www.tek-tips.com/faqs.cfm?fid=1958但你的方式更好。 – MAW74656 2010-10-05 19:59:45

回答

40

SET需要來查詢的FROM\JOIN\WHERE部分之前。

UPDATE CE 
SET sJobNumber = AD.JobNumber 
FROM CostEntry CE 
    INNER JOIN ActiveCostDetails As AD 
     ON CE.lUniqueID = AD.UniqueID 
WHERE CE.SEmployeeCode = '002' 
    AND SubString(CostCentre, 1, 1) = sDepartmentCode 
    AND substring(CostCentre, 3, 1) = sCategoryCode 
    AND substring(CostCentre, 5, 2) = sOperationCode 
+0

+1:[SS2000更新文檔](http://msdn.microsoft.com/en-us/library/aa260662%28SQL.80%29.aspx) – 2010-10-05 19:54:40

+0

@Joe Stefanelli - 關閉,但解析器不喜歡設置別名時的名稱。 – MAW74656 2010-10-05 19:58:23

+0

這工作:UPDATE CE SET CE.sJobNumber = AD.JobNumber FROM CostEntry CE INNER JOIN ActiveCostDetails作爲AD ON CE.lUniqueID = AD.UniqueID WHERE CE.SEmployeeCode = '002' 和substring(CostCentre,1 ,1)= sDepartmentCode AND子串(CostCentre,3,1)= sCategoryCode AND子串(CostCentre,5,2)= sOperationCode – MAW74656 2010-10-05 19:58:41

1

這應該工作

UPDATE CE 
SET CostEntry.sJobNumber = ActiveCostDetails.JobNumber 
FROM CostEntry CE 
INNER JOIN ActiveCostDetails As AD ON CostEntry.lUniqueID = ActiveCostDetails.UniqueID  
    WHERE CostEntry.SEmployeeCode = '002' 
     AND SubString(CostCentre, 1, 1) = sDepartmentCode 
     AND substring(CostCentre, 3, 1) = sCategoryCode 
     AND substring(CostCentre, 5, 2) = sOperationCode 
+2

哇,老問題....非常尷尬,我不知道這回當時... – MAW74656 2017-05-18 16:23:31