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'
精彩评论