MERGE 语句错误

MERGE statement ERRORS

当 运行 我的 MERGE 语句

时出现此错误
Error report -
SQL Error: ORA-00904: "SRC"."TB2"."WAERS": invalid identifier
00904. 00000 -  "%s: invalid identifier".

这是我的声明:

MERGE INTO costing. TB1 USING
(SELECT costing.TB1.ROWID row_id,
        costing.TB2.WAERS
   FROM costing.TB2
   JOIN costing.TB3
     ON costing.TB2.BUKRS = costing.TB3.BUKRS
   JOIN costing.TB1
     ON costing.TB3.MANDT = Client
    AND costing.TB3.BWKEY = costing.TB1.Plant
  WHERE Currency_Conversion_Status <> 'Pricing Missing'
) src ON ( costing.TB1.ROWID = src.row_id )
WHEN MATCHED THEN
  UPDATE SET Currency = src.TB2.WAERS;

您正试图在子查询外使用您在 src 子查询中定义的别名。那永远行不通。

我建议使用 table 别名使列引用更易于查看 - 我会这样做:

merge into costing.tb1 tgt
using (select t1.rowid row_id,
              t2.waers
       from   costing.tb2 t2
              join costing.tb3 t3 on t2.bukrs = t3.bukrs
              join costing.tb1 t1 on t3.mandt = client -- missing alias on client
                                  and t3.bwkey = t1.plant
       where  currency_conversion_status <> 'Pricing Missing' -- missing alias on currency_conversion_status
      ) src 
  on (tgt.rowid = src.row_id )
when matched then
  update set tgt.currency = src.waers;

我建议您也添加缺少的来自客户端和 currency_conversion_status 的别名,这样您就可以分辨出它们属于哪个 table。

如果 client 是 costing.tb1 中的一个列,那么您可能不需要在子查询中包含对 tb1 的连接,您可以直接在 MERGE 语句中连接它,例如:

merge into costing.tb1 tgt
using (select t3.mandt,
              t3.bwkey,
              t2.waers
       from   costing.tb2 t2
              join costing.tb3 t3 on t2.bukrs = t3.bukrs
       where  currency_conversion_status <> 'Pricing Missing' -- missing alias on currency_conversion_status
      ) src 
  on (tgt.client = src.mandt
      and tgt.plant = src.bwkey)
when matched then
  update set tgt.currency = src.waers;

尽管因为我不知道 table currency_conversion_status 属于哪个 - 您可能需要移动 where 子句所在的位置。

通过这样做,您将删除对 costing.tb1 table 的不必要连接,这应该会提高语句的性能。