Skip to content
Advertisement

Update column in Oracle table with value from another table with duplicates

I am trying to update the column (REPT_IND) from table A to the value in table B where A.ID = B.ID and some conditions in table B. There are some duplicates in table B, but nonetheless the REPT_IND is the same and I still need the value. How can I do this on Oracle? Any tips are appreciated thank you!

The Following code has the Error:

ORA-01427: single-row subquery returns more than one row

Code:

UPDATE A
SET REPT_IND= (
                        SELECT B.REPT_IND
                        FROM  B
                        INNER JOIN A
                            ON B.ID = A.ID
                        where A.ID = B.ID
                        and B.job_type = 'P'
                        and B.FT_PT is not null
                        );

Advertisement

Answer

You can try also merge statement:

merge into a 
using (
    select a.id,max(b.rept_ind) rept_ind
    from  a left join b on a.id=b.id
    where b.job_type = 'p'
      and b.ft_pt is not null
) b
on (a.id=b.id)
when matched then update 
    set a.rept_ind=b.rept_ind;

Or if you do not want to set a.rept_ind to null if there is no relevant rows in b:

merge into a 
using (
    select b.id, max(b.rept_ind) rept_ind
    from  b
    where 
            b.job_type = 'p'
        and b.ft_pt is not null
    group by b.id
) b
on (a.id=b.id)
when matched then update 
    set a.rept_ind=b.rept_ind;
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement