需要简化Oracle 11g中的更新查询

时间:2014-01-22 19:08:37

标签: sql oracle

如果有可能,请帮助简化以下更新查询,似乎包括额外的不必要的选择:

UPDATE TARGET a
SET a.ACCOUNTID = (SELECT x.SFDC_ID
                     FROM   SOURCE x
                   WHERE  a.ACCOUNTID = x.LEGACY_SYSTEM_ID__C
                      AND x.SFDC_ID is not null) 
WHERE  a.ACCOUNTID = (SELECT LEGACY_SYSTEM_ID__C
                       FROM   SOURCE x
                      WHERE  a.ACCOUNTID = x.LEGACY_SYSTEM_ID__C
                         AND x.SFDC_ID is not null)

2 个答案:

答案 0 :(得分:0)

这个怎么样?

UPDATE 
(SELECT a.ACCOUNTID , x.SFDC_ID
FROM TARGET a
INNER JOIN SOURCE x 
ON a.ACCOUNTID = x.LEGACY_SYSTEM_ID__C
  AND x.SFDC_ID is not null
) t
SET t.ACCOUNTID = t.SFDC_ID

答案 1 :(得分:0)

使用“存在”条件执行此操作的一种方法。

 UPDATE TARGET a
    SET a.ACCOUNTID = (SELECT x.SFDC_ID
                         FROM   SOURCE x
                       WHERE  a.ACCOUNTID = x.LEGACY_SYSTEM_ID__C
                          AND x.SFDC_ID is not null) 
    WHERE  exists (SELECT 1
                           FROM   SOURCE x
                          WHERE  a.ACCOUNTID = x.LEGACY_SYSTEM_ID__C
                             AND x.SFDC_ID is not null)