开发者

Update Table from a subquery

开发者 https://www.devze.com 2023-01-30 08:36 出处:网络
In the statement below, I\'m trying to normalize LocationIDs in the table \'Replies\' based off the data I retrieve from the subquery SELECT statement. Basically, there are LocationIDs in the Replies

In the statement below, I'm trying to normalize LocationIDs in the table 'Replies' based off the data I retrieve from the subquery SELECT statement. Basically, there are LocationIDs in the Replies table that are not in the MasterList, I would like to replace those occurances with the location of '1234'. I figured the statement below would work, but it doesn't. When I attempt to run it, it updates all the LocationID's in the Replies tables after May 5th, 2开发者_如何学JAVA010.

UPDATE Replies
SET Replies.LocationID = '1234'
FROM (SELECT lml.LocationID FROM Replies sfs LEFT JOIN MasterList lml ON lml.LocationID=sfs.LocationID WHERE sfs.CreateDate >= '5/5/2010') AS rs
WHERE rs.LocationID is null


You can use the not exists clause to find locations that do not exists on masterList as

update replies  
set locationid='1234'
where not exists (
select 1 from masterlist as ml
where 
   ml.locationid=replies.locationid
)
and CreateDate >= '5/5/2010'
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号