I have two tables:
table1
: (ID, Code, Name)
table2
: (ID, Code, Name)
with same columns
I want to to insert data from table1 to table2 or update columns if that exists in table2 (table1.ID = table2.ID)
What is the simple way to do this?
WHITOUT MERGE
Here I am writing a script that use full when you want to update table2 from table1.
And if you want to insert then use this script.
If in table2 id is not auto increment. Otherwise not insert the value of id column in table2.
There are some issues with Merge statement,so it should be used with caution..
Further i recommend ,using merge as two seperate DML statements like below..
Reasons being stated by Paul White here in his detailed answer..
Assuming the ID column is unique and should not be set, it seems you could do it in two SQL Statements.
get all rows that are in table1 but not in table2
update table2
It might be worth looking at triggers on update and insert if you'd like to have this done manually