Update Multiple Rows in Oracle: Bulk Methods

In Oracle, to update multiple rows of data in bulk, one of the following methods can be used:

  1. By using the MERGE statement, you can achieve insertion, updating, and deletion operations. You can merge multiple update statements into one MERGE statement to batch update multiple records. The following is an example code snippet.
MERGE INTO table_name dest
USING (SELECT id, new_value FROM updated_data) src
ON (dest.id = src.id)
WHEN MATCHED THEN UPDATE SET dest.column_name = src.new_value;
  1. You can use a PL/SQL loop to update data one by one and then batch execute multiple update operations through the FORALL statement. Here is an example code:
DECLARE
    TYPE id_table IS TABLE OF table_name.id%TYPE INDEX BY PLS_INTEGER;
    TYPE value_table IS TABLE OF table_name.column_name%TYPE INDEX BY PLS_INTEGER;
    
    ids id_table;
    values value_table;
BEGIN
    -- 填充ids和values数组
    
    FOR i IN 1..ids.COUNT LOOP
        UPDATE table_name
        SET column_name = values(i)
        WHERE id = ids(i);
    END LOOP;
END;

Both methods can be used to achieve batch updating multiple records in an Oracle database.

bannerAds