我的劇本的失敗已經用MERGE語句幾年,每一天,但在10月6日就停止工作,並拋出一個錯誤:MERGE語句後2年以上工作
Msg 8672, Level 16, State 1, Line 1
The MERGE statement attempted to UPDATE or DELETE the same row more than once. This happens when a target row matches more than one source row. A MERGE statement cannot UPDATE/DELETE the same row of the target table multiple times. Refine the ON clause to ensure a target row matches at most one source row, or use the GROUP BY clause to group the source rows.
我的腳本看起來像:
MERGE INTO dbo.energydata AS target
USING dbo.temp_energydata AS source ON target.webmeterID = source.webmeterID
AND target.DateTime = source.DateTime
WHEN MATCHED THEN
UPDATE
SET target.kWh = source.kWh
WHEN NOT MATCHED BY TARGET THEN
INSERT (webmeterID, DateTime, kWh)
VALUES (source.webmeterID, source.DateTime, source.kWh);
它基本上從temp_table(temp_energydata)獲取數據,並與主表(energydata)合併。它使用合併,因爲臨時表每天都包含重複數據,並且MERGE
將防止重複錯誤。
我意識到我將不得不提供更多的信息,所以如果你可以讓我知道如何調試,這將是偉大的。
你必須在臨時表副本,即兩個(或更多)的相同值的記錄'webmeterId'和'DateTime'。 – Guffa 2014-10-27 22:15:14
謝謝,你有腳本的想法找到重複? – user1745767 2014-10-27 22:40:47