Good Morning,
Can you please help with below code.
FINALTable is the target table that needs appends/updates from #SourceTable(which is a daily refresh from feed).
here the MID, MName, MDOB is compbined data key / composite key so if the combination of three columns values exist in Target then if there is any other column values between source and target then need to update Target with source values
(
when the target table column having value and source is null or empty then no need to update on the columns (if record exists)
to optimize, update only the records, when there is changes (this could be flexible)
)
if the composite key not exist then need to insert whole record to the Target.
if there is no difference between source and taget record then ignore the record without any insertion/update in the target table.
--Target Table sample
CREATE TABLE #FINALTable (MID varchar(20), MName Varchar(50), MDOB DATE, Addr Varchar(50), City Varchar(25), ZIP Varchar(10), Phone Varchar(10))
INSERT INTO #FINALTable values ('101','AAA','9/18/1993','123 Victor BLVD', 'Kansas', '08909','2431231210')
INSERT INTO #FINALTable values ('102','BBB','12/18/1994','123 Victor Park', 'Kansas', '77834','2431231211')
INSERT INTO #FINALTable values ('103','CCC','7/10/1995','1893 SuLv RD', 'Kansas', '34534','2431231234')
INSERT INTO #FINALTable values ('104','DDD','07/09/1996','3 PARK', 'Kansas', '44444','2431231214')
INSERT INTO #FINALTable values ('105','EEE','7/08/1997','999 Merol DR', 'Kansas', '42424','2431231233')
INSERT INTO #FINALTable values ('106','FFF','7/10/1998','8 MOTT AVE', 'Kansas', '99234','8431231234')
--Source Table sample below incomments each record explained what it suppose to Do.
CREATE TABLE #SourceTable (MID varchar(20), MName Varchar(50), MDOB DATE, Addr Varchar(50), City Varchar(25), ZIP Varchar(10), Phone Varchar(10))
INSERT INTO #SourceTable values ('101','AAA','9/18/1993','123 Victor BLVD', 'Kansas', '08909','2431231210')
--- MID 101(the compkey MID, NAME, MDOB) exist in Target and no other column values changed in src vs target so no action need to take..
INSERT INTO #SourceTable values ('102','BBBJJJ','12/18/1994','123 Victor Park', 'Kansas', '77834','2431231211')
---MID 102 the Composite key (MID, NAME, MDOB) is changed for name column, the comp key is not exist in target, so new record need to be inserted, without updating 102
INSERT INTO #SourceTable values ('103','CCC','7/10/1995','1893 SuLv RD', 'NewYork', '34534','2431231234')
-- MID 103 the City name changed so master record need to update for 103 with Newyork value
INSERT INTO #SourceTable values ('104','DDD','07/09/1996','100 Manhatten PARK', 'Kansas', '44444','2222224444')
--MID 104 compkey exist in target ('104','DDD','07/09/1996'), phone no change and address change so need to update to target
INSERT INTO #SourceTable values ('111','EEE','7/08/1997','999 Merol DR', 'Kansas', '42424','2431231233')
---MID 111 ('111','EEE','7/08/1997') is not exist in the Target table so need to insert into target
INSERT INTO #SourceTable values ('106','XXX','7/10/1998','8 MOTT AVE', 'Kansas', '99234','8431231234')
--MID 106 composit key column changed which is not exist in Target ('106','XXX','7/10/1998') so need to insert as a new record
Please help
Thanks in advance
ASiti