简体   繁体   中英

PL/SQL Update multiple rows after comparing to different table

couldn't find anything similar on google or through searching through this site so i figured i'd ask.

What I'm trying to do is update a global temp table's (named GTT) rows in a column called Ent_Name with rows from a column named Modified_Name in table MN_Xref wherever OG_Name (also from MN_Xref) = GTT.Ent_Name. If there is no OG_Name that matches with Ent_Name then Ent_Name should not be updated. GTT has already been populated at this point. How could this be done?

I can provide more info if need be. Thanks!

Here for this kind of scenario MERGE will be one of the good options. Hope below snippet helps.

MERGE INTO GTT USING MN_REF
ON ( gtt.ENT_NAME = MN_REF.OG_NM)
WHEN MATCHED THEN 
UPDATE SET
ENT_NAME = MN_REF.MODIFIED_NAME;

This should work for you

UPDATE 
(
   select GTT.Ent_Name          old_name
        , MN_Xref.Modified_Name new_name
   FROM GTT INNER JOIN MN_Xref ON (GTT.Ent_Name = MN_Xref.OG_Name)
)
SET old_name = new_name;

if i got your problem correctly this should solve it

    declare 
  cursor c is
select * from MN_Xref;

begin
for i in c loop
update GTT g
   set g.Ent_Name = i.Modified_Name
 where g.Ent_Name = i.OG_Name;
commit;
end loop;
end;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM