Update multiple table column values using single query

后端 未结 4 1953
一生所求
一生所求 2020-12-21 08:13

How would you update data in multiple tables using a single query?

MySQL Example

The equivalent code in MySQL:

UPDATE party p
LEF         


        
4条回答
  •  难免孤独
    2020-12-21 08:34

    You could use Oracle MERGE statement to do this. It is a bulk update-or-insert kind of statement based on joining the target table with an inline view.

    MERGE INTO bonuses D
       USING (
          SELECT employee_id, salary, department_id FROM employees
          WHERE department_id = 80
       ) S ON (D.employee_id = S.employee_id)
     WHEN MATCHED THEN 
       UPDATE SET D.bonus = D.bonus + S.salary*.01
     WHEN NOT MATCHED THEN 
       INSERT (D.employee_id, D.bonus)
       VALUES (S.employee_id, S.salary*0.1);
    

    if you do not need the insert part, you just omit the last 3 lines above.

提交回复
热议问题