从子查询更新多行

时间:2015-09-18 21:17:55

标签: sql-server

我正在尝试从子查询更新多行。请注意,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 个答案:

答案 0 :(得分: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