I have two tables as followed:
Table A (with Column 1A and Column 2A) Table B (with Column 1B and Column 2B)
I want to write a procedure that helps me do the following:
- Find matched rows of Table A and Table B, where 1A = 1B
- After that, for each of the matched rows, if 2B is null, then update it with 2A; if it is not null, then not update it.
I am a newbie in SQL and Oracle, I would appreciate any help.
Thank you in advance
Advertisement
Answer
Try this MERGE
statement:
MERGE INTO TABLE_B B USING (SELECT 1A, 2A FROM TABLEA) A ON (A.1A = B.1B) WHEN MATCHED THEN UPDATE SET B.2B = COALESCE(B.2B, A.2A);
Cheers!!