数据库有一个表A和一个表B. A.ID与B.ID链接。
当且仅当这些实例链接到A.ID = B.ID时,我需要将A.Name的所有实例更新为等于B.Catalog。
只应更新A.Owner = 0的A中的条目。
我提出的代码是:
UPDATE A
SET A.Name = (SELECT B.Catalog
FROM B, A
WHERE (B.ID = A.ID) AND (A.Owner = 0))
WHERE A.ID IN (SELECT B.ID
FROM B, A
WHERE (B.ID = A.ID) AND (A.Owner = 0))
错误是"Subquery returned more than one value"
(MS SQL 2005添加"MSG 512, LEVEL 16, STATE 1, LINE 1"
)。我理解错误告诉我的是,我正在尝试将单个值设置为等于SELECT语句返回的多个结果,但我认为我对SQL的了解太缺乏,无法提供正确的代码。 / p>
非常感谢任何帮助。这就是我尝试自学的方法!头疼!
答案 0 :(得分:4)
update A
set A.Name = B.Catalog
from TableA A
join TableB B on
A.Id = B.Id
where A.Owner = 0
答案 1 :(得分:3)
UPDATE A
SET A.Name = B.Catalog
FROM A INNER JOIN B ON A.ID = B.ID
WHERE A.Owner = 0