简体   繁体   English

比较不同的表后,PL / SQL更新多行

[英]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. 无法在Google上找到任何类似内容,也无法通过搜索该网站找到任何类似内容,所以我想问一下。

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. 我想做的是使用MN_Xref表中名为Modified_Name的列(无论OG_Name(也来自MN_Xref))= GTT.Ent_Name的行,更新名为Ent_Name的列中的全局临时表(名为GTT)的行。 If there is no OG_Name that matches with Ent_Name then Ent_Name should not be updated. 如果没有OG_Name与Ent_Name匹配,则不应更新Ent_Name。 GTT has already been populated at this point. 此时,GTT已被填充。 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. 在这种情况下,MERGE将是不错的选择。 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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