0

I am using the following query in oracle. However, it gives an error saying that "c.par" in line 5 is an invalid parameter. No idea why. The columns exist. I checked. I have been struggling with this for a long time. All I want to do is to merge one table into another and update it using oracle. Could someone please help?

MERGE INTO SPRENTHIERARCHIES 
USING ( SELECT c.PARENTCATEGORYID AS par,  
             e.rootcategoryId AS root 
        FROM  SPRENTCATEGORIES c,SPRENTHIERARCHIES e 
        WHERE e.root (+)= c.par 
      ) SPRENTCATEGORIES  
ON (SPRENTHIERARCHIES.rootcategoryId = SPRENTCATEGORIES.parentcategoryId) 
WHEN MATCHED THEN 
UPDATE SET e.root=c.par 
4

1 回答 1

2

和别名仅存在于子句e中的查询中。您试图在子句中引用它们。您还针对没有该列的目标表使用子句中的列别名(除非您的表同时具有 rootcategoryId 和 root,以及 parentCategoryId 和 par)。cusingupdateusing

所以这:

UPDATE SET e.root=c.par 

应该:

UPDATE SET SPRENTHIERARCHIES.rootcategoryId= SPRENTCATEGORIES.par 

在该using子句中,您尝试将列别名用作同一级别的查询,因此:

    WHERE e.root (+)= c.par 

应该:

    WHERE e.rootcategoryId (+)= c.PARENTCATEGORYID

您的on子句也是错误的,因为它没有使用列别名:

ON (SPRENTHIERARCHIES.rootcategoryId = SPRENTCATEGORIES.par) 

但我建议你using用适当的连接子句替换子句中的旧语法:

MERGE INTO SPRENTHIERARCHIES 
USING ( SELECT c.PARENTCATEGORYID AS par,  
             e.rootcategoryId AS root 
        FROM  SPRENTCATEGORIES c
        LEFT JOIN SPRENTHIERARCHIES e 
        ON e.rootcategoryId = c.PARENTCATEGORYID 
      ) SPRENTCATEGORIES  
ON (SPRENTHIERARCHIES.rootcategoryId = SPRENTCATEGORIES.par) 
WHEN MATCHED THEN 
UPDATE SET SPRENTHIERARCHIES.rootcategoryId= SPRENTCATEGORIES.par  

但是,当您尝试更新连接列时,您有一个更根本的问题;这将得到:

ORA-38104: Columns referenced in the ON Clause cannot be updated

正如 Gordon Linoff 建议的那样,您可以使用更新而不是合并。就像是:

UPDATE SPRENTHIERARCHIES h
SET h.rootcategoryId = (
  SELECT c.PARENTCATEGORYID
  FROM SPRENTCATEGORIES c
  WHERE c.PARENTCATEGORYID = h.rootCategoryID
)
WHERE EXISTS (
  SELECT null
  FROM SPRENTCATEGORIES c
  WHERE c.PARENTCATEGORYID = h.rootCategoryID
)

where exists如果没有匹配的记录,则该子句存在-原始查询中的外部联接暗示了这一点。但在这种形式中,更明显的是您将更新rootcategoryId为相同的值,因为您选择的 parentCategoryID等于它。所以更新(或合并)似乎毫无意义。

于 2016-06-08T14:08:00.553 回答