我的剧本的失败已经用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