是否可以将这两个更新查询合并为1 这样做有没有任何优势?
update AREA
set ID_MasterArea =AREACITY.ID_Area
from dbo.Area AREA
JOIN dbo.Address A on AREA.ID_Address = A.ID_Address
JOIN dbo.Area AREACITY on A.ID_City = AREACITY.ID_City
where AREA.ID_MasterArea IS NULL
and AREA.ID_Address IS NOT NULL
update AREA
set ID_MasterArea =AREACOUNTRY.ID_Area
from dbo.Area AREA
JOIN dbo.City C on AREA.ID_City = c.ID_City
JOIN dbo.Area AREACOUNTRY on C.ID_Country = AREACOUNTRY.ID_Country
where AREA.ID_MasterArea IS NULL
and AREA.ID_City IS NOT NULL
答案 0 :(得分:1)
你看起来像这样吗?
update AREA
set ID_MasterArea = case when Area.ID_Address is not null then AREACOUNTRY.ID_Area
when Area.ID_City is not null then AreaCountry.Id_Area
end
from dbo.Area AREA
JOIN dbo.City C on AREA.ID_City = c.ID_City
LEFT JOIN dbo.Area AREACOUNTRY on C.ID_Country = AREACOUNTRY.ID_Country
LEFT JOIN dbo.Address A On Area.ID_Address = A.ID_Address
LEFT JOIN dbo.AREA AREACITY on A.ID_City = AREACITY.ID_City
where AREA.ID_MasterArea IS NULL
通过选择要更新的记录来检查更新前的连接,并在相应的情况下进行相应的更新