Multi-row Inserts in Oracle
Using the INSERT ALL Statement:
INSERT ALL INTO table_name (column1, column2, ...) VALUES (value1, value2, ...) INTO table_name (column1, column2, ...) VALUES (value1, value2, ...) ... SELECT * FROM dual;
- **Explanation:**
- The `INSERT ALL` statement allows you to insert multiple rows into one or more tables in a single statement.
- Each `INTO` clause specifies a target table and its columns.
- The `VALUES` clause provides the data for each row.
- The `SELECT * FROM dual` clause is often used as a placeholder to indicate that the statement is an insert operation.
- **Example:**
```sql
INSERT ALL
INTO employees (employee_id, first_name, last_name, hire_date) VALUES (100, 'John', 'Doe', TO_DATE('01-JAN-2024', 'DD-MON-YYYY'))
INTO employees (employee_id, first_name, last_name, hire_date) VALUES (101, 'Jane', 'Smith', TO_DATE('15-FEB-2024', 'DD-MON-YYYY'))
SELECT * FROM dual;
Using a PL/SQL Block:
-
Example:
DECLARE CURSOR employee_cursor IS SELECT employee_id, first_name, last_name, hire_date FROM employees; employee_rec employee_cursor%ROWTYPE; BEGIN OPEN employee_cursor; LOOP FETCH employee_cursor INTO employee_rec; EXIT WHEN employee_cursor%NOTFOUND; -- Insert the row using employee_rec values END LOOP; CLOSE employee_cursor; END;
-
Explanation:
- A PL/SQL block provides more flexibility for complex insert operations.
- You can declare variables to store data and use loops or other control structures to iterate over rows and insert them.
-
DECLARE -- Declare variables BEGIN -- Insert rows using a loop or other logic END;
Using a Bulk Collect and Forall Statement:
-
DECLARE TYPE employee_type IS TABLE OF employees%ROWTYPE; employee_collection employee_type; BEGIN SELECT * BULK COLLECT INTO employee_collection FROM employees; FORALL i IN 1..employee_collection.COUNT INSERT INTO employees_archive (employee_id, first_name, last_name, hire_date) VALUES (employee_collection(i).employee_id, employee_collection(i).first_name, employee_collection(i).last_name, employee_collection(i).hire_date); END;
-
- This method is particularly efficient for inserting a large number of rows.
- You first bulk collect rows into a collection and then use a
FORALL
statement to insert them in a single operation.
-
DECLARE -- Declare a collection to store rows BEGIN -- Bulk collect rows into the collection FORALL i IN 1..collection.COUNT INSERT INTO table_name (column1, column2, ...) VALUES (collection(i).column1, collection(i).column2, ...); END;
Choosing the Best Method:
- For more complex inserts or large datasets, PL/SQL blocks or bulk collect/forall statements can be more efficient.
- For simple inserts with a small number of rows, the
INSERT ALL
statement is often sufficient. - The best method depends on factors such as the number of rows to be inserted, the complexity of the insert operation, and performance requirements.
Understanding Multi-Row Inserts in Oracle
INSERT ALL
INTO table_name (column1, column2, ...) VALUES (value1, value2, ...)
INTO table_name (column1, column2, ...) VALUES (value1, value2, ...)
...
SELECT * FROM dual;
INSERT ALL
INTO customers (customer_id, first_name, last_name) VALUES (101, 'John', 'Doe')
INTO customers (customer_id, first_name, last_name) VALUES (102, 'Jane', 'Smith')
SELECT * FROM dual;
- The
SELECT * FROM dual
clause is often used as a placeholder to indicate that the statement is an insert operation. - The
VALUES
clause provides the data for each row. - Each
INTO
clause specifies a target table and its columns. - The
INSERT ALL
statement allows you to insert multiple rows into one or more tables in a single statement.
DECLARE
-- Declare variables
BEGIN
-- Insert rows using a loop or other logic
END;
DECLARE
TYPE customer_type IS TABLE OF customers%ROWTYPE;
customer_collection customer_type;
BEGIN
-- Populate the collection with customer data
SELECT * BULK COLLECT INTO customer_collection FROM customers;
-- Insert rows using a FORALL loop
FORALL i IN 1..customer_collection.COUNT
INSERT INTO customers_archive (customer_id, first_name, last_name) VALUES (customer_collection(i).customer_id, customer_collection(i).first_name, customer_collection(i).last_name);
END;
- In this example, we use a bulk collect to efficiently fetch multiple rows into a collection and then insert them using a
FORALL
loop.
- Consider factors such as performance requirements, data volume, and the complexity of your insert logic when selecting the appropriate method.
- For more complex inserts or large datasets, PL/SQL blocks can be more efficient, especially when using bulk collect and
FORALL
.
Alternative Methods for Multi-Row Inserts in Oracle
While the methods discussed previously (using INSERT ALL
and PL/SQL blocks) are common and effective, there are a few other alternatives worth considering:
Using a Dynamic SQL Statement
- Example:
DECLARE stmt VARCHAR2(4000); BEGIN stmt := 'INSERT INTO customers (customer_id, first_name, last_name) VALUES (:1, :2, :3)'; EXECUTE IMMEDIATE stmt USING 101, 'John', 'Doe'; END;
- When to use: When you need to dynamically construct the insert statement based on runtime conditions or user input.
Using a Stored Procedure
- Example:
CREATE PROCEDURE insert_customers ( customer_id IN NUMBER, first_name IN VARCHAR2, last_name IN VARCHAR2 ) AS BEGIN INSERT INTO customers (customer_id, first_name, last_name) VALUES (customer_id, first_name, last_name); END;
- When to use: When you need to encapsulate the insert logic into a reusable unit and potentially perform additional operations.
Using a Materialized View Log
- Example:
CREATE MATERIALIZED VIEW LOG ON customers; CREATE TABLE customers_archive AS SELECT * FROM customers; CREATE TRIGGER customers_archive_trg AFTER INSERT ON customers REFERENCING NEW AS new ROW FOR EACH ROW BEGIN INSERT INTO customers_archive VALUES new; END;
- When to use: When you need to track changes to a table and efficiently insert new rows based on those changes.
Key considerations for choosing a method:
- Specific requirements: If you need to track changes or perform additional operations, materialized view logs or triggers may be appropriate.
- Maintainability: Stored procedures can improve code organization and maintainability.
- Flexibility: Dynamic SQL and stored procedures offer more flexibility, especially when dealing with varying conditions or complex logic.
- Performance: For large datasets, bulk insert methods like PL/SQL blocks with bulk collects and
FORALL
are often the most efficient.
sql oracle sql-insert