我想我已经看这个声明太久了。谁能告诉我为什么这个声明的更新部分没有更新?我认为通过在列上进行测试or
,!=
这应该只在需要时调用更新的原因——相反,我似乎无意中创建了一个条件......永远不需要更新!
在查看语句之前,需要了解以下 4 件事:
1. 临时表#Facilities 中的数据填充了来自多个来源的最新数据。此数据包含机器人新(插入)和更改(更新)数据。
2. 在执行 Merge 语句之前,#Facilities 的内容已被验证包含数据。
3. 合并语句的插入部分成功。
4. 虽然语句的更新部分不会更新更改的数据,但语句不会失败。
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
);