Oracle SQL MERGE INTO给出了ORA-00918模糊定义的错误

时间:2014-07-22 19:17:54

标签: sql oracle oracle11g

您好我从SQL Server迁移到Oracle,我遇到了一些使用内部联接进行简单更新的查询问题。我将我的情景与我在这里和其他地方找到的任何例子进行了比较,我只是没有看到问题出在哪里。

以下是SQL Server版本:

UPDATE Rep_AcctWalk_CurrentMnth
SET Rep_AcctWalk_CurrentMnth.Business_L1 = List_BusOrg.Business_L1, 
    Rep_AcctWalk_CurrentMnth.Business_L2 = List_BusOrg.Business_L2, 
    Rep_AcctWalk_CurrentMnth.Business_L3 = List_BusOrg.Business_L3, 
    Rep_AcctWalk_CurrentMnth.Business_L4 = List_BusOrg.Business_L3, 
    Rep_AcctWalk_CurrentMnth.Business_Segment = List_BusOrg.Business_Segment
FROM PSME.CapCostFore.Rep_AcctWalk_CurrentMnth 
INNER JOIN PSME.Glob.List_BusOrg 
ON LTRIM(RTRIM(Rep_AcctWalk_CurrentMnth.Bus_Code)) = List_BusOrg.Bus_Code

Oracle尝试:

   MERGE INTO CAPREPACCTWALKCURMNTH CAP1
   USING (SELECT CAP2.ROWID, BUS.Business_L1, BUS.Business_L2, BUS.Business_L3, 
      BUS.Business_L3, BUS.Business_Segment
   FROM CAPREPACCTWALKCURMNTH CAP2
          JOIN GLOBLISTBUSORG BUS   
          ON (CAP2.Bus_Code = BUS.Bus_Code )
   ) src
   ON ( CAP1.ROWID = src.ROWID )
   WHEN MATCHED THEN UPDATE SET CAP1.Business_L1 = src.Business_L1,
                                CAP1.Business_L2 = src.Business_L2,
                                CAP1.Business_L3 = src.Business_L3,
                                CAP1.Business_L4 = src.Business_L3,
                                CAP1.Business_Segment = src.Business_Segment;

2 个答案:

答案 0 :(得分:2)

我不太清楚为什么要将UPDATE转换为MERGE。 Oracle中的相关UPDATE看起来像这样

UPDATE Rep_AcctWalk_CurrentMnth currMnth
SET (Business_L1, Business_L2, Business_L3,
     Business_L4, Business_Segment ) = 
        (SELECT Business_L1, 
                Business_L2,
                Business_L3,
                Business_L4,
                Business_Segment 
           FROM PSME.Glob.List_BusOrg busOrg
          WHERE trim( currMnth.bus_code ) = busOrg.busCode)

如果您打算更新Rep_AcctWalk_CurrentMnth表或

中的每一行
UPDATE Rep_AcctWalk_CurrentMnth currMnth
SET (Business_L1, Business_L2, Business_L3,
     Business_L4, Business_Segment ) = 
        (SELECT Business_L1, 
                Business_L2,
                Business_L3,
                Business_L4,
                Business_Segment 
           FROM PSME.Glob.List_BusOrg busOrg
          WHERE trim( currMnth.bus_code ) = busOrg.busCode)
 WHERE EXISTS( SELECT 1
                 FROM PSME.Glob.List_BusOrg busOrg
                WHERE trim( currMnth.bus_code ) = busOrg.busCode )

如果您只想更新匹配的行。

如果您想使用MERGE

MERGE INTO CAPREPACCTWALKCURMNTH CAP
   USING (SELECT BUS.Business_L1, 
                 BUS.Business_L2, 
                 BUS.Business_L3, 
                 BUS.Business_L4, 
                 BUS.Business_Segment
            FROM GLOBLISTBUSORG BUS   
   ) src
   ON ( CAP.Bus_Code = src.Bus_Code )
   WHEN MATCHED THEN UPDATE SET CAP.Business_L1 = src.Business_L1,
                                CAP.Business_L2 = src.Business_L2,
                                CAP.Business_L3 = src.Business_L3,
                                CAP.Business_L4 = src.Business_L4,
                                CAP.Business_Segment = src.Business_Segment;

答案 1 :(得分:0)

除了Justin的评论 - 这是写这个的所有替代方法 - 问题是内部查询的rowid值,它需要是别名,然后在merge ON语句中使用 - 这是一个讨厌的小怪癖,但你想知道的一个。

MERGE INTO CAPREPACCTWALKCURMNTH CAP1
   USING (SELECT CAP2.ROWID as row_id, BUS.Business_L1, BUS.Business_L2, BUS.Business_L3, 
      BUS.Business_L3, BUS.Business_Segment
   FROM CAPREPACCTWALKCURMNTH CAP2
          JOIN GLOBLISTBUSORG BUS   
          ON (CAP2.Bus_Code = BUS.Bus_Code )
   ) src
   ON ( CAP1.ROWID = src.row_id )
   WHEN MATCHED THEN UPDATE SET CAP1.Business_L1 = src.Business_L1,
                                CAP1.Business_L2 = src.Business_L2,
                                CAP1.Business_L3 = src.Business_L3,
                                CAP1.Business_L4 = src.Business_L3,
                                CAP1.Business_Segment = src.Business_Segment;