Tabella 1:
id name desc
-----------------------
1 a abc
2 b def
3 c adf
Tabella 2:
id name desc
-----------------------
1 x 123
2 y 345
In oracolo SQL, come posso eseguire una query sql update che può aggiornare la tabella 1 con la tabella 2 utilizzando lo stesso id
? Quindi il risultato finale che otterrei è
Tabella 1:
id name desc
-----------------------
1 x 123
2 y 345
3 c adf
La domanda è presa da https://stackoverflow.com/questions/5036918/sql-update-query-with-data-from-another-table, ma specificamente per oracle SQL.
Questo è chiamato un aggiornamento correlato
UPDATE table1 t1
SET (name, desc) = (SELECT t2.name, t2.desc
FROM table2 t2
WHERE t1.id = t2.id)
WHERE EXISTS (
SELECT 1
FROM table2 t2
WHERE t1.id = t2.id )
Assumendo che l'unione risulti in una vista con conservazione delle chiavi, si potrebbe anche
UPDATE (SELECT t1.id,
t1.name name1,
t1.desc desc1,
t2.name name2,
t2.desc desc2
FROM table1 t1,
table2 t2
WHERE t1.id = t2.id)
SET name1 = name2,
desc1 = desc2
Qui sembra essere una risposta ancora migliore con 'in' clausola che permette di avere chiavi multiple per il join:
update fp_active set STATE='E',
LAST_DATE_MAJ = sysdate where (client,code) in (select (client,code) from fp_detail
where valid = 1) ...
L'esempio completo è qui: http://forums.devshed.com/oracle-development-96/how-to-update-from-two-tables-195893.html
Il manzo sta nell'avere le colonne che vuoi usare come chiave tra parentesi nella clausola where prima di 'in' e avere la dichiarazione select con gli stessi nomi di colonna tra parentesi. where (colonna1,colonna2) in ( select (colonna1,colonna2) from table where "the set I want" );