Skip to content
Advertisement

Match rows from 2 table and update value of 1 column from 1 table to the other table if the value in the corresponding column is null

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:

  1. Find matched rows of Table A and Table B, where 1A = 1B
  2. 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!!

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement