我認爲我一直在看這種說法太長了。誰能告訴我爲什麼這個聲明的更新部分沒有更新?我認爲,通過在列上測試or
和!=
,這應該只在需要時調用更新的原因 - 相反,我似乎無意中創建了一個條件..其中不需要更新!SQL Server:合併聲明的更新部分不起作用
在查看聲明之前,需要了解以下4件事情:
1.臨時表#Facilities中的數據使用來自多個來源的最新數據填充。此數據包含bot新(插入)和更改(更新)數據。
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
);
爲什麼你需要MERGE這個?你認爲它會給你帶來什麼(除了更復雜和難以調試的語法)?似乎你可以通過一個'UPDATE WHERE EXISTS/INSERT WHERE NOT EXISTS'模式來實現這個更簡單。 – 2013-05-13 19:44:50
@Aaron,爲什麼不使用合併的東西呢?我認爲語法不一定更難。在我認爲應該工作和爲什麼不工作之間存在差距。關於爲什麼這個邏輯不起作用的任何想法? – plditallo 2013-05-13 19:51:48
我沒有精力看邏輯,但這裏是我爲什麼不使用MERGE(除了不可能記憶的語法之外):http://www.sqlperformance.com/2013/02/t -sql-queries/another-merge-bug(請閱讀全文)。 – 2013-05-13 19:54:24