- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2025 01:44 AM - edited 05-14-2025 01:49 AM
In your scenario at update section, you are trying to update primary keys aswell, which Delta Table can't differentiate when you re-run the same batch/file and throws error as all duplicates, to run without error/fail, remove (Target.Distributor_ID = Source.Distributor_ID, Target.Customer_ID = Source.Customer_ID) columns from UPDATE clause. Updated code below.
MERGE INTO slvr_masterdata.Customer_Master as Target
USING preprocessed_source AS Source
ON Source.Key_ID = Target.Key_ID
WHEN MATCHED THEN
UPDATE SET
Target.Customer_Name = Source.Customer_Name,
Target.Channel = Source.Channel,
Target.Time_Stamp = current_timestamp()
WHEN NOT MATCHED
THEN INSERT
(
Distributor_ID,
Customer_ID,
Customer_Name,
Channel,
Time_Stamp
)
VALUES (
Source.Distributor_ID,
Source.Customer_ID,
Source.Customer_Name,
Source.Channel,
current_timestamp()
)