Hi Su Wang ,
Welcome to Microsoft Q&A platform and thanks for posting your query here.
I understand that you are facing an error : '##InterimTable_209cbcef-08cd-4b73-9967-e49cad4bfcc2' in the Azure SQL Database, but that table already exists' while performing upsert in the database.
- This can happen when the pipeline is run multiple times, and the temporary table is not being dropped properly between runs.
- When you select the write behavior to be INSERT, the pipeline simply adds new rows to the target table without checking for duplicates. However, when you change the write behavior to UPSERT, the pipeline first creates a temporary table to hold the incoming data, then performs a merge operation to insert new rows and update existing rows in the target table based on the specified key column. This is likely why you see the error when trying to perform an UPSERT but not when performing an INSERT.
You can try following approaches:
1. Try to add a step in the pre copy script of copy data activity to drop the temporary table , in case it exists from a previous run.
IF OBJECT_ID(N'##InterimTable_209cbcef-08cd-4b73-9967-e49cad4bfcc2') IS NOT NULL
BEGIN
DROP TABLE ##InterimTable_209cbcef-08cd-4b73-9967-e49cad4bfcc2
END
GO
2. You can also try to TRUNCATE the base table and then try to perform UPSERT once.
Checkout this video on how to perform upsert for incremental records
Hope it helps. Please do consider clicking Accept Answer and Up-Vote for the same as accepted answers help community as well.