I have source and table destination tables like this
Source:
Column1 Column2 Column3 Column4
Destination:
Column1 Column2 Column5 Column6
How can I implement this logic in SQL SERVER
IF SOURCE.Column1 = DESTINATION.Column1 AND SOURCE.Column2 = DESTINATION.Column2
UPDATE DESTINATION Column5 = SOURCE.Column1 + 12 (some other logic)
ELSE
Do something else
The issue is, that I have to deal with billion rows, so what is the best way to implement the above logic
EDIT1
IF SOURCE.Column1 = DESTINATION.Column1 AND SOURCE.Column2 = DESTINATION.Column2
UPDATE DESTINATION Column5 = SOURCE.Column1 + 12 (some other logic)
ELSE
**INSERT SOURCE table row to the destination**
How can I implement the same with merge statement, because I have to consider only two columns
You can
UPDATEwithJOINand that would be more performance wise in your case. Like so:You can use the
MERGEstatement like so:Note that:
In the
INSERTstatement there was noINTO TableName, because the name of the target table is already defined in theMERGEclause, which isSOURCE. Thats why I definedSOURCE AS TGTandDESTINATION AS SRC.It is mandatory to end the
MERGEstatement with semicolon. And it is best practice to do so with al sql statements.