如何使用其他 table 和函数更新 table1 字段

How to update table1 field by using other table and function

我有两个table和一个函数,

表 1 包含 shop_code,batch_id,registry_id

shop_code| batch_id|registry_id   
123 | 100  |12
124 | 100  |13
125 | 100  |12

表 2 contains shop_code,shop_name

shop_code| shop_name   
123 | need to populate  
124 | need to populate  
125 | need to populate

Function1 从 table1 和 returns shop_name

中获取参数 registry_id

Table2 shop_name 是空的我想填充 shop_code。

我已经尽力了,但所有的努力都是徒劳的。
如果有人可以帮助我使用 Oracle,那就太好了。

我尝试了下面的代码,但是从关键字

给出了错误
 update TABLE2 set T2.SHOP_NAME = T.SHOP_NAME
 from(
 select GET_shop_name(t1.registry_id) as shop_name ,
           t1.shop_code shop_code
           from TABLE1 T1 
                ) t where t.shop_code = t1.shop_code;

您需要 MERGE 语句。

类似这样的方法可能有效:

MERGE INTO TABLE2 t2
USING (
  SELECT GET_shop_name(t1.batch_id) AS shop_name ,
  t1.shop_code shop_code
  FROM TABLE1 T1 ) t1
ON (t2.shop_code = t1.shop_code)
WHEN MATCHED THEN
  UPDATE SET t2.shop_name = t1.shop_name
;

如果上面的确切代码不起作用,您将不得不原谅我现在没有 SQL Dev 语法细节。 :)

我不能完全 100% 确定我是否答对了你的问题,但我相信你想要的是

update
  table2 u
set
  shop_name = (
    select
      get_shop_name(t1.batch_id)
    from
      table1 t1
    where
      t1.chop_code = u.shop_code
   ); 

你能试试这个方法吗?尝试将内部查询放入商店名称值;我没有测试过,但我认为方法对你有用。

update TABLE2 T2
   set T2.SHOP_NAME =
       (select GET_shop_name(t1.batch_id, t1.shop_code) from table1 t1 wehre t1.shop_code = t2.shop_code)
 where T2.shop_name is null