I want to push all data from SourceTable to TargetTable by inserting or updating accordingly. I don’t need any entries removing from TargetTable, even if they are not present in SourceTable.
This is what I have so far. Is the MERGE command the most suitable approach for this? I’ve had a look at some example but not quite sure on how to go about this.
My current solution truncates the whole table and then inserts of the data. I have since realised I need to keep the PK values for values that existed previously in the TargetTable. How do I change this to INsert or update depending on whether AssociatedRefId exists in the PublicRefId column of the TargetTable?
-- Truncate lancrm01sql's Baseline Official table
TRUNCATE TABLE [TargetTable]
-- Populate table
INSERT INTO [TargetTable]
(PublicRefId
,FamilyName
,GivenName
)
SELECT
AssociatedRefId
, lastname
, firstname
FROM
#TempSourceTable
This article gives a pretty good explanation on using MERGE.