Oracle查询更新自

时间:2018-12-20 02:25:46

标签: sql oracle

我想知道是否可以在Oracle数据库中执行此查询吗?

UPDATE
    Table_A SET
    Table_A.col1 = Table_B.col1,
    Table_A.col2 = Table_B.col2 FROM
    Some_Table AS Table_A
    INNER JOIN Other_Table AS Table_B
        ON Table_A.id = Table_B.id WHERE
    Table_A.col3 = 'cool'

这正在sql server(Microsoft)上运行。但不能在oracle db中工作。你能告诉我原因吗?

3 个答案:

答案 0 :(得分:1)

您可以简单地执行以下操作:

UPDATE table_a SET table_a.col1 = (SELECT table_B.COl1
                                  FROM table_B 
                                  WHERE table_a.id = table_b.id),
                   table_a.col2 = (SELECT table_B.COl2
                                  FROM table_B 
                                  WHERE table_a.id = table_b.id)
WHERE table_a.col3='cool';

答案 1 :(得分:1)

您可以在Oracle中使用merge into。

MERGE Into Table_A t USING Table_B s
           ON (t.id = s.id)
        when matched then UPDATE SET
    t.col1 = s.col1, t.col2 = s.col2   
       WHERE t.col3 = 'cool'

答案 2 :(得分:0)

这是一种可行的方法:

UPDATE 
(
    SELECT a.col1 AS col1a, a.col2 AS col2a, b.col1 AS col1b, b.col2 AS col2b
    FROM Some_Table a
    INNER JOIN Other_Table b
        ON a.id = b.id
    WHERE a.col3 = 'cool'
) t
SET
    a.col1a = b.col1b,
    a.col2a = b.col2b;

如果Oracle不想运行上面的命令,那么您将不得不使用相关的子查询。