Thanks Tommy for this great procedure.
But there's a problem if you want to merge a table with more than 2 columns in the PK.
If you change
SELECT @pk_2 = 'AND ' + @local_schema_name + '_TARGET.' + column_name + ' = ' + @local_schema_name + '_SOURCE.' + column_name
FROM @pk_table WHERE key_seq = 2
into
SET @pk_2 = '';
SELECT @pk_2 = @pk_2 + ' AND ' + @local_schema_name + '_TARGET.' + column_name + ' = ' + @local_schema_name + '_SOURCE.' + column_name
FROM @pk_table WHERE key_seq > 1;
you can also handle tables with more than 2 columns as a PK.
Best regards
Helmut