2011-08-23 70 views
0

我有以下查詢,它適用於MySQL,我需要幫助&提示爲Oracle 11g重寫它。 (我是新來的PL/SQL ..)在Oracle中使用兩個表更新Oracle中的表

update table1 n, table2 t 
set n.col1 = t.col1, n.col2 = t.col2 
where t.col3 = n.col3; 
+0

你只是在你的聲明中更新一個表,TABLE1 – Ollie

+0

@Ollie,你說得對,我會寫的問題,針對不同的查詢,但想通了.. –

回答

1
UPDATE table1 n 
    SET (n.col1, n.col2) = (SELECT t.col1, t.col2 
          FROM table2 t 
          WHERE t.col3 = n.col3) 
WHERE EXISTS (SELECT 'x' 
       FROM table2 t 
       WHERE t.col3 = n.col3); 
+0

我不t得到'WHERE EXISTS(SELECT'x'' ...部分,是否只是過濾出't.col3 = n.col3'的行? –

+0

這是爲了將行更新限制爲只有t的行。 col3 = n.col3。 – Ollie

2

嘗試類似這樣的事情。

UPDATE 
    (
     SELECT n.col1 AS n_col1, n.col2 AS n_col2, t.col1 AS t_col1, t.col2 AS t_col2 
     FROM table1 n 
     JOIN tabble2 t ON n.col3=t.col3 
) a 
SET a.n_col1 = a.t_col1, 
    a.n_col2 = a.t_col2