sql server 2008 r2 - The MERGE statement attempted to UPDATE or DELETE the same row more than once -
i have source table 3 fields
declare @tmptvp table ( id int, startdate datetime, enddate datetime ) insert @tmptvp values(1,'2013-04-28','2013-04-30') insert @tmptvp values(2,'2013-05-01','2013-06-30') select * @tmptvp target table 'insertdate' contains following data. insert insertdate values(1,'2013-04-28','2013-06-30') while merging merge (@tmptvp) tgt using (select id,startdate,enddate @tmptvp) src on (tgt.startdate = src.startdate or tgt.enddate = src.enddate) when matched update set tgt.startdate = case when src.startdate < tgt.startdate src.startdate else tgt.startdate end, tgt.enddate = case when src.enddate > tgt.enddate src.enddate else tgt.enddate end, when not matched insert (id,startdate,enddate) values (src.id,src.startdate,src.enddate);
above merge statement throws merge statement attempted update or delete same row more once. happens when target row matches more 1 source row. merge statement cannot update/delete same row of target table multiple times. refine on clause ensure target row matches @ 1 source row, or use group clause group source rows.
can helps
please check site
http://www.sqlsafety.com/?p=665
the merge statement attempted update or delete same row more once. happens when target row matches more 1 source row. merge statement cannot update/delete same row of target table multiple times. refine on clause ensure target row matches @ 1 source row, or use group clause group source rows.” several times , forced run through query logic see has occurred. reading message self explanatory has happened. row in target table affected more once within merge statement causes query fail.
best regards
Comments
Post a Comment