Last post May 22, 2019 10:48 AM by me_ritz
May 22, 2019 07:22 AM|pughal_dotmvc|LINK
Thanks in Advance.
I need to load the data from another database table to my new database in SQL.
I am using MERGE statement to do the task in same time.
Below one is my current statement to do merging
MERGE TableTarget t
USING TableSource s
ON (s.category_id = t.category_id)
THEN UPDATE SET
t.category_name = s.category_name,
t.amount = s.amount
WHEN NOT MATCHED BY TARGET
THEN INSERT (category_id, category_name, amount)
VALUES (s.category_id, s.category_name, s.amount)
WHEN NOT MATCHED BY SOURCE
But ,When not matched by target I need to delete the values of all referenced dependent tables and insert new values into parent table and reference tables with new keys at the same time WHEN NOT MATCHED BY TARGET.
Could you please assist me this Can I do in Merge statment or Is any other way to do the same task?
May 22, 2019 10:48 AM|me_ritz|LINK
WHEN NOT MATCHED BY TARGET only supports INSERT statement.
You would want to do insert into target table, and then taking reference from there do other operations.
Alternatively, You may also use OUTPUT clause along with MERGE statement to do the task. Here is a link: https://www.sqlservercentral.com/articles/the-output-clause-for-the-merge-statements