Unique Constraint Violation on Merge
686856Mar 25 2010 — edited May 20 2010I'm getting a unique constraint violation on the following but I don't understand why. I know there are duplicates on remote.customer_id but I'm doing a distinct on that column but still the error. In my local table, customer_id is the primary key so it must be unique. If I remove the primary key constraint altogether, the error goes away.
Can anyone tell me why both DISTINCT and UNIQUE fail to eliminate duplicates in this merge statement? I have confirmed that remote.customer_id is the source of the duplicates.
merge into CUSTOMER c
using (
select DISTINCT remote.customer_id,
remote.name_1,
remote.name_2,
remote.name_3,
remote.name_4,
remote.house_number_and_street,
remote.city,
remote.region,
remote.country_code,
remote.postal_code,
p.customer_id_of_bus_partner
from D_CUSTOMER@BW_LINK remote
left join D_CUSTOMER_PARTNER_FUNCTIONS@BW_LINK p
on p.customer_id = remote.customer_id
where p.partner_function_id = 'ZS'
) remote
on (c.customer_id = remote.customer_id)
when matched then
update set c.name_1 = remote.name_1,
c.name_2 = remote.name_2,
c.name_3 = remote.name_3,
c.name_4 = remote.name_4,
c.house_number_and_street = remote.house_number_and_street,
c.city = remote.city,
c.region = remote.region,
c.country_code = remote.country_code,
c.postal_code = remote.postal_code
when not matched then
insert (c.customer_id,
c.name_1,
c.name_2,
c.name_3,
c.name_4,
c.house_number_and_street,
c.city,
c.region,
c.country_code,
c.postal_code,
c.customer_id_of_bus_partner
)
values (remote.customer_id,
remote.name_1,
remote.name_2,
remote.name_3,
remote.name_4,
remote.house_number_and_street,
remote.city,
remote.region,
remote.country_code,
remote.postal_code,
remote.customer_id_of_bus_partner
)
LOG ERRORS INTO DML_ERROR_LOG ('Customer Merge') REJECT LIMIT 5;
Edited by: bjiggs on Mar 25, 2010 10:56 AM