I think I've been looking at this statement way too long. Can anyone tell me why the update portion of this statement isn't updating? I think that by testing with or
and !=
on the columns, this should invoke a cause for updates only when needed--instead, I seem to have unintentionally created a condition ..where the update is never needed!
Here are 4 things to know prior to looking at the statement:
1. The data in the temp table #Facilities is populated with the most current data from multiple sources. This data contains bot new (insert) and changed (update) data.
2. The contents of #Facilities has been verified to contain the data, prior to executing the Merge statement.
3. The insert portion of the merge statement succeeds.
4. Although the update portion of the statement does not update changed data, the statement does not fail.
MERGE INTO PhoneMaster AS facilitymaster
USING #Facilities as facilitynew
ON facilitymaster.facilityid = facilitynew.facilityid
AND facilitymaster.siteid = facilitynew.siteid
WHEN MATCHED AND
(
facilitymaster.User__bID != facilitynew.User__bID
or facilitymaster.Email__baddress != facilitynew.Email__baddress
or facilitymaster.Facility__bName != facilitynew.Facility__bName
or facilitymaster.Division != facilitynew.Division
or facilitymaster.District != facilitynew.District
) THEN
UPDATE SET
facilitymaster.User__bID = facilitynew.User__bID,
facilitymaster.Email__baddress = facilitynew.Email__baddress,
facilitymaster.Facility__bName = facilitynew.Facility__bName,
facilitymaster.Division = facilitynew.Division,
facilitymaster.District = facilitynew.District
WHEN NOT MATCHED THEN
INSERT
( User__bID,
Email__baddress,
Facility__bName,
Division,
District,
FacilityId,
SiteId
)
VALUES
(
facilitynew.User__bID,
facilitynew.Email__baddress,
facilitynew.Facility__bName,
facilitynew.Division,
facilitynew.District,
facilitynew.FacilityId,
facilitynew.SiteId
);