Oracle SQL: aktualizacja tabeli danymi z innej tabeli

Tabela 1:

id    name    desc
-----------------------
1     a       abc
2     b       def
3     c       adf

Tabela 2:

id    name    desc
-----------------------
1     x       123
2     y       345

W oracle SQL, jak uruchomić zapytanie SQL update, które może zaktualizować tabelę 1 za pomocą tabeli 2 name i desc używając tego samego id? Tak więc efekt końcowy jaki uzyskam to

Tabela 1:

id    name    desc
-----------------------
1     x       123
2     y       345
3     c       adf

Pytanie pochodzi z zaktualizuj jedną tabelę danymi z innej, ale specjalnie dla oracle SQL.

Author: Laxmi, 2011-08-11

7 answers

To się nazywa skorelowana aktualizacja

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 )

Zakładając, że połączenie spowoduje zachowanie klucza, możesz również

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
 539
Author: Justin Cave,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-08-11 19:05:21

Spróbuj tego:

MERGE INTO table1 t1
USING
(
-- For more complicated queries you can use WITH clause here
SELECT * FROM table2
)t2
ON(t1.id = t2.id)
WHEN MATCHED THEN UPDATE SET
t1.name = t2.name,
t1.desc = t2.desc;
 161
Author: Adrian,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-23 20:22:48

Try

UPDATE Table1 T1 SET
T1.name = (SELECT T2.name FROM Table2 T2 WHERE T2.id = T1.id),
T1.desc = (SELECT T2.desc FROM Table2 T2 WHERE T2.id = T1.id)
WHERE T1.id IN (SELECT T2.id FROM Table2 T2 WHERE T2.id = T1.id);
 20
Author: Yahia,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-08-11 18:08:49
Update table set column = (select...)

Nigdy nie działało dla mnie, ponieważ set oczekuje tylko 1 wartości-błąd SQL: ORA-01427: jednorzędowe zapytanie podrzędne zwraca więcej niż jeden wiersz.

Oto rozwiązanie:

BEGIN
For i in (select id, name, desc from table1) 
LOOP
Update table2 set name = i.name, desc = i.desc where id = i.id;
END LOOP;
END;

Tak dokładnie uruchamiasz go na arkuszu sqldeveloper. Mówią, że to powolne, ale to jedyne rozwiązanie, które mi pomogło w tej sprawie.

 9
Author: Pau Karr,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-06-27 03:04:53

Tutaj wydaje się być jeszcze lepszą odpowiedzią z klauzulą 'in', która pozwala na wiele kluczy dla połączenia :

update fp_active set STATE='E', 
   LAST_DATE_MAJ = sysdate where (client,code) in (select (client,code) from fp_detail
  where valid = 1) ...

Pełny przykład znajduje się tutaj: http://forums.devshed.com/oracle-development-96/how-to-update-from-two-tables-195893.html - z archiwum internetowego, ponieważ link nie działa.

The beef is in have the columns that you want to use as the key in nawiases in the where clause before ' in ' and have the select statement with the same column names in w nawiasach. where ( column1, column2) in (select ( column1, column2) from table where "the set I want" );

 8
Author: ant,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2021-01-19 11:16:46

Jeśli twoja tabela t1 i kopia zapasowa T2 mają wiele kolumn, oto kompaktowy sposób na to.

Ponadto mój problem polegał na tym, że tylko niektóre kolumny zostały zmodyfikowane i wiele wierszy nie miało żadnych zmian w tych kolumnach, więc chciałem zostawić je w spokoju - zasadniczo przywrócić podzbiór kolumn z kopii zapasowej całej tabeli. Jeśli chcesz przywrócić wszystkie wiersze, pomiń klauzulę where.

Oczywiście prostszym sposobem byłoby kasowanie i wstawianie jako select, ale w moim przypadku I potrzebne rozwiązanie z tylko aktualizacje.

Sztuczka polega na tym, że gdy wybierzesz * z pary tabel o zduplikowanych nazwach kolumn, druga otrzyma nazwę _1. Oto co wymyśliłem:

  update (
    select * from t1 join t2 on t2.id = t1.id
    where id in (
      select id from (
        select id, col1, col2, ... from t2
        minus select id, col1, col2, ... from t1
      )
    )
  ) set col1=col1_1, col2=col2_1, ...
 -3
Author: Jim P,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-06-14 17:03:06
BEGIN
For i in (select id, name, desc from table2) 
LOOP
Update table1 set name = i.name, desc = i.desc where id = i.id and (name is null or desc is null);
END LOOP;
END;
 -3
Author: Avila Theresa,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-20 17:37:49