'INSERT * in MERGE

I am trying to automate a data read into SQL. The data supplier recommended using a MERGE update on the PK supplied as per the below:

MERGE TargetTable AS trg
   USING SourceData AS src
      ON (trg.[FKCS] = src.[FKCS])
      WHEN NOT MATCHED BY TARGET
        THEN
            INSERT (Everything)
            VALUES (Everything)

      WHEN MATCHED AND src.Col1 <> trg.Col1
                    OR src.Col2 <> trg.Col2
                    OR src.IsDeleted <> trg.IsDeleted
                    OR src.IsUpdated <> trg.IsUpdated
                    OR src.LastDateUpdated <> trg.LastDateUpdated
         THEN UPDATE SET
                  TargetData.[Column] = SourceData.[Column]

I am using a python script to run this process and need to do it for 8 tables with different column names. Is there anyway I can somehow automate this without needing to hard code the column names or should I rather use a pandas approach?

I'd also like to substitute the "Everything" with something like SELECT * FROM SOURCEDATA



Solution 1:[1]

Unfortunately, the columns need to be specified and as I was not prepared to go that route, I opted for a pandas approach.

Pandas turned out to be an elegant solution and I managed to achieve the goal using a left merge and keeping only "left_only" records.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 E P