在Oracle中使用内部联接进行更新无法正常工作

时间:2018-07-11 11:29:48

标签: oracle ms-access sql-update

将查询从Access转换为Oracle无效

访问权限

UPDATE 
     Table1 
INNER JOIN      
     Table2
ON  (Table1.ID = Table2.ID) AND (Table1.AREA = Table2.AREA) 
SET Table2.LAT = Table1.LAT, Table2.LON = Table1.LON;   

Oracle

UPDATE 
    Table1 
SET
    Table2.LAT= (SELECT Table1.LAT FROM Table1  WHERE Table1.ID = Table2.ID  AND Table1.AREA = Table2.AREA );

在Oracle中获取此错误

错误报告-

SQL Error: ORA-00904: "Table2"."LAT": invalid identifier
00904. 00000 -  "%s: invalid identifier"

在Oracle中寻找正确的等效项。

1 个答案:

答案 0 :(得分:2)

我将使用合并语句,例如:

merge into table2 tgt
  using table1 src
    on (tgt.id = src.id and tgt.area = src.area)
when matched then
  update set tgt.lat = src.lat,
             tgt.lon = src.lon;

应该比相应的更新语句具有更高的性能:

UPDATE table2
SET    (table2.lat, table2.lon) = (SELECT table1.lat,
                                          table1.lon
                                   FROM   table1
                                   WHERE  table1.id = table2.id
                                   AND    table1.area = table2.area)
WHERE  EXISTS (SELECT NULL
               FROM   table1
               WHERE  table1.id = table2.id
               AND    table1.area = table2.area);

由于更新需要重复两次子查询以确保它仅更新正确的行,而合并会在两个表上进行联接。