2015-09-18 82 views
-1

我試圖從子查詢更新多行。請注意,ID是表中的主鍵。從子查詢中更新多行

這裏是我的查詢:

SELECT tm.ID, tm.LocID = (SELECT CASE COUNT(*) WHEN 1 THEN MAX(LocID) END 
       FROM tblLoc tl 
       WHERE tl.LocID = tm.LodID) 
FROM tblMain tm 

這裏是我的更新語句:

Update tblMain 
set LocID = TMP.LocID 
(
    SELECT tm.ID, tm.LocID = (SELECT CASE COUNT(*) WHEN 1 THEN MAX(LocID) END 
       FROM tblLoc tl 
       WHERE tl.LocID = tm.LodID) 
    FROM tblMain tm 

) AS TMP 
where tblMain.ID = TMP.ID 

我得到不正確的語法。

回答

1
Update tblMain 
set LocID = TMP.LocID 
FROM tblMain INNER JOIN 
(
    SELECT tm.ID, tm.LocID = (SELECT CASE COUNT(*) WHEN 1 THEN MAX(LocID) END 
       FROM tblLoc tl 
       WHERE tl.LocID = tm.LodID) 
    FROM tblMain tm 

) AS TMP 
ON tblMain.ID = TMP.ID 
+0

可能你的別名tblMain用,比如,男,所以它的更新M集...從間內加入...上m.id = tmp.id – Beth

+0

是你能做到這一點,但錯誤是在語法上,所以我只是修復了。 –