MERGE语句错误

时间:2015-07-20 12:44:32

标签: sql oracle merge ora-00904

运行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;

1 个答案:

答案 0 :(得分:1)

您尝试使用在子查询外部的src子查询中定义的别名。那永远无法发挥作用。

我建议使用表别名来使列引用更容易看到 - 这就是我要做的事情:

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中添加缺少的别名,以便告诉他们属于哪些表。

如果客户端是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;

虽然因为我不知道哪个表currency_conversion_status属于 - 你可能需要移动where子句所在的位置。

通过这样做,您可以删除对costing.tb1表的不必要的连接,这样可以提高语句的性能。