Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Oracle SQL: Insert Multiple Rows Into a Table

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

You can insert multiple rows into an Oracle table in one statement using either the multi-row INSERT ALL syntax or a single INSERT ... SELECT ... UNION ALL query. The INSERT ALL form is the most readable for a handful of literal rows and works in all supported Oracle Database versions; the UNION ALL form is a portable fallback when you need a single INSERT with a SELECT source. Both approaches are faster than running many separate single-row INSERT statements because they reduce round trips and parse overhead.

What You'll Need

  • Access to an Oracle Database instance (any supported version, including Oracle Database Free, Express Edition, or a cloud instance).
  • A user account with the INSERT privilege on the target table, or the INSERT ANY TABLE system privilege.
  • A SQL client such as SQL*Plus, SQL Developer, SQLcl, or any JDBC/ODBC-based tool that can run a script.
  • The target table must already exist, and you need to know its column names, data types, and any NOT NULL or check constraints.

Step-by-Step

  1. Connect to the database. Open your SQL client and connect as the schema owner or a user with insert rights. For example, in SQL*Plus run CONNECT username/password@service_name. If you are using SQL Developer, create a connection and open a SQL Worksheet.
  2. Confirm the table structure. Run DESCRIBE your_table; (or query USER_TAB_COLUMNS) to list column names and data types. This prevents column-order mistakes and tells you which columns are mandatory.
  3. Choose your insert style. Use INSERT ALL for literal values that are easy to read. Use INSERT ... SELECT ... UNION ALL if you are pulling rows from another query, or if your client or tool has trouble with INSERT ALL. Both are single statements and both are valid Oracle SQL.
  4. Write the INSERT ALL statement. The pattern is: INSERT ALL, then one INTO table (columns) VALUES (...) clause per row, then a final SELECT * FROM dual; to satisfy Oracle's requirement that INSERT ALL be followed by a subquery. For example:
    INSERT ALL
      INTO employees (employee_id, first_name, last_name, hire_date)
        VALUES (101, 'Alice', 'Nguyen', DATE '2024-01-15')
      INTO employees (employee_id, first_name, last_name, hire_date)
        VALUES (102, 'Ben', 'Okafor', DATE '2024-02-01')
      INTO employees (employee_id, first_name, last_name, hire_date)
        VALUES (103, 'Carla', 'Mendes', DATE '2024-03-10')
    SELECT * FROM dual;
    
    The SELECT * FROM dual line is not optional: without it Oracle raises an error because INSERT ALL expects a subquery source.
  5. Or write the UNION ALL form. This is a single INSERT whose source is a set of SELECT ... FROM dual rows joined by UNION ALL:
    INSERT INTO employees (employee_id, first_name, last_name, hire_date)
    SELECT 101, 'Alice', 'Nguyen', DATE '2024-01-15' FROM dual
    UNION ALL
    SELECT 102, 'Ben', 'Okafor', DATE '2024-02-01' FROM dual
    UNION ALL
    SELECT 103, 'Carla', 'Mendes', DATE '2024-03-10' FROM dual;
    
    This form works in every Oracle version and is easy to generate programmatically.
  6. Run the statement. Execute it in your SQL client. Oracle reports the number of rows inserted (for example, 3 rows inserted.). If you are using SQL*Plus or SQLcl, end the statement with a semicolon and press Enter; in SQL Developer, use the Run Statement button (F9) or Run Script (F5) for scripts.
  7. Commit the transaction. Oracle does not auto-commit by default. Run COMMIT; to make the inserts permanent, or ROLLBACK; if you spot a mistake. Many GUI clients have an auto-commit toggle; check its state before running large inserts.
  8. Verify the rows. Query the table, for example SELECT * FROM employees WHERE employee_id IN (101,102,103);, and confirm the values, dates, and nulls are as expected.

Common Mistakes

  • Omitting the final SELECT in INSERT ALL. Oracle requires a subquery after the INTO clauses; SELECT * FROM dual is the standard filler.
  • Forgetting to commit. The rows appear in your session but disappear for other users and after disconnect until you run COMMIT.
  • Mismatched column counts or types. Each INTO ... VALUES clause must supply values for exactly the listed columns, in the same order, with compatible data types. A string in a NUMBER column fails at parse or execute time.
  • Using INSERT ALL with a VALUES clause that references a sequence incorrectly. If you need sequence values, call seq.NEXTVAL once per row; do not assume a single call populates all rows.
  • Exceeding the 1000-element IN limit when verifying. Oracle limits an IN list to 1000 expressions; split larger checks or use a join.
  • Assuming INSERT ALL is atomic per row. The statement is one transaction; a failure rolls back the whole statement unless you handle errors with LOG ERRORS.

Tips

  • Use the DATE 'YYYY-MM-DD' literal syntax for dates to avoid NLS_DATE_FORMAT surprises.
  • For large batches (thousands of rows), prefer INSERT ... SELECT from an external table, a staging table, or use SQL*Loader; multi-row INSERT ALL is convenient but not a bulk loader.
  • Add the LOG ERRORS INTO err$_table REJECT LIMIT UNLIMITED clause (after creating an error log table with DBMS_ERRLOG.CREATE_ERROR_LOG) to capture bad rows without failing the whole statement.
  • Wrap the statement in a transaction and validate with a SELECT COUNT(*) before committing.
  • Keep column lists explicit in every INTO clause; relying on positional inserts breaks when the table gains a column.

Troubleshooting

  • ORA-00933: SQL command not properly ended. Usually a missing SELECT * FROM dual after INSERT ALL, or a stray semicolon inside the statement.
  • ORA-00947: not enough values. The number of values does not match the number of columns in an INTO clause. Recount both lists.
  • ORA-01861: literal does not match format string. A date or timestamp string is being converted implicitly. Use DATE '...' or TO_DATE with an explicit format.
  • ORA-00001: unique constraint violated. One of the rows duplicates an existing key. Remove or update the duplicate, or use a merge strategy.
  • Rows inserted but not visible to others. You have not committed. Run COMMIT; in the same session.
  • Statement hangs or runs long. Check for locks on the table from another session (V$LOCK, V$SESSION) and confirm you are not inserting into a table with a long-running trigger.

Frequently Asked Questions

Can I insert multiple rows with a single INSERT in Oracle?

Yes. Use INSERT ALL ... SELECT * FROM dual or INSERT INTO ... SELECT ... UNION ALL .... Both are single statements and both are standard Oracle SQL.

Is there a limit on how many rows INSERT ALL can handle?

There is no small hard limit like 1000; the practical limit is memory and undo. For very large batches, use SQL*Loader, external tables, or INSERT ... SELECT from a staging table.

Why do I need SELECT * FROM dual at the end of INSERT ALL?

Oracle's grammar requires INSERT ALL to be followed by a subquery that supplies the source rows. SELECT * FROM dual returns one row and satisfies the syntax without adding data.

Does INSERT ALL commit automatically?

No. Oracle does not auto-commit DML by default. You must run COMMIT to persist the rows; otherwise a rollback or disconnect discards them.

Can I use a sequence with INSERT ALL?

Yes, but call sequence_name.NEXTVAL in each INTO clause where you need a new value. A single call does not automatically populate every row.

What is the difference between INSERT ALL and INSERT FIRST?

INSERT ALL inserts into every INTO clause whose condition is true (or unconditionally). INSERT FIRST evaluates conditions in order and inserts only into the first matching clause, which is useful for conditional routing.