我有一個要更新的建筑物串列,通過從另一個表中提取另一個欄位,我有一個它們的 pk/fk 識別符號串列。我喜歡一次完成所有操作,而不是拋出幾十個單獨的更新陳述句,每個更新陳述句都有一個 id。
似乎是一個如此簡單的任務,我很肯定它必須很容易,我只是還沒有看到如何正確地完成它。SQL 似乎討厭在更新時使用“where x in ()”。
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
這就是我要運行的內容。
update accountinginfo
set companyname = (select facilityname from facility)
where facilityid in (12345,12346,12347)
由于缺乏更好的術語,通過這樣的記錄而不使用游標,“回圈”的正確方法是什么?
樣本記錄
uj5u.com熱心網友回復:
您還可以使用聯合更新:
update a
set companyname = f.facilityname
from accountinginfo a
join facility f on f.facilityid = a.facilityid
where f.facilityid in (12345, 12346, 12347);
uj5u.com熱心網友回復:
您的子查詢需要系結到外部查詢。
update a
set companyname = (select facilityname f from facility WHERE f.facilityid = a.facilityid)
FROM accountinginfo a
where a.facilityid in (12345,12346,12347)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/431800.html
上一篇:從子查詢中的多個值中選擇一個特定值selectinselect子句
下一篇:如何將列添加到現有SQL輸出?
