我想我一直在看这个陈述太久了。谁能告诉我为什么这个声明的更新部分没有更新?我认为通过在列上使用or
和!=
进行测试,这应该仅在需要时调用更新的原因 - 相反,我似乎无意中创建了一个条件..更新永远不会需要!
在查看声明之前,有四件事需要了解:
1.临时表#Facilities中的数据填充了来自多个源的最新数据。此数据包含bot new( insert )和更改( update )数据。
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
);
答案 0 :(得分:1)
我注意到的一件事是你的插入语句没有填充FacilityID或SiteID。我要提出的另一个建议是在WHEN MATCHED上注释你的'AND'条款,一次加入一行直到它失败。我一直使用MERGE,它非常快速有效。