Oracle SQL: Update a table with data from another table

后端 未结 7 2139
广开言路
广开言路 2020-11-22 02:01

Table 1:

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

Table 2:

id    na         


        
7条回答
  •  抹茶落季
    2020-11-22 02:12

    Update table set column = (select...)
    

    never worked for me since set only expects 1 value - SQL Error: ORA-01427: single-row subquery returns more than one row.

    here's the solution:

    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;
    

    That's how exactly you run it on SQLDeveloper worksheet. They say it's slow but that's the only solution that worked for me on this case.

提交回复
热议问题