FB

How to Insert Multiple Rows in SQL

Do you need to insert multiple rows into an SQL database?

You could do that separately using INSERT statements, but there are a few problems with this.

Learn what these problems are and how to insert multiple rows in SQL in this article.

Problem

You’ve got a bunch of data to insert into a table. This could be a small number of rows (say, 3 or 4) or there could be tens or hundreds of rows.

One way to get the data in is to write separate INSERT statements.

For example:

INSERT INTO customer (first_name, last_name) VALUES ('Kristen', 'Rowley');
INSERT INTO customer (first_name, last_name) VALUES ('Jed', 'Tomlinson');
INSERT INTO customer (first_name, last_name) VALUES ('Margie', 'Escobar');
INSERT INTO customer (first_name, last_name) VALUES ('Harriette', 'Mejia');
INSERT INTO customer (first_name, last_name) VALUES ('Francis', 'Little');

This will get the data into the table.

However, there are a couple of problems with this method.

 

It’s slow

Running separate INSERT statements can be quite slow. Each statement has to be processed and executed separately. Data is written to the table separately.

All of this overhead can make it slow when you want to insert a lot of records.

It’s a lot of code

When you write separate INSERT statements like this, there is a lot of repeated code. The INSERT INTO, table name, and columns are all repeated.

Ideally, we would not have to repeat this.

Errors can cause half-entered data

If you get an error halfway through your series of INSERT statements, the row with the error is not inserted, but all rows before it are inserted.

You’ll need to fix the error and rerun the statements, but only run the statements that did not insert.

Running only part of a statement could be a good thing, as you get some of the result, but it can take time to find the point of failure and run the right commands.

Let’s take a look at how we can insert multiple rows in SQL.

 

SQL Insert Multiple Rows

Here’s an example of batch inserting rows in SQL. It will work for MySQL, SQL Server, and PostgreSQL. It won’t work for Oracle as they don’t support this format, but there’s another way you can do this in Oracle which I’ve detailed later in this article.

Let’s create a simple customer table with two fields:

CREATE TABLE customer (
  first_name VARCHAR(100),
  last_name VARCHAR(100)
);

Now, we can INSERT multiple rows in SQL by repeating the list of values inside the brackets:

INSERT INTO customer (first_name, last_name) VALUES
('Kristen', 'Rowley'),
('Jed', 'Tomlinson'),
('Margie', 'Escobar'),
('Harriette', 'Mejia'),
('Francis', 'Little');

We have a single INSERT INTO command, and specify the columns to insert into once. We then specify the keyword VALUES. Finally, we add each of the rows we want to insert inside brackets, separated by a comma.

This should insert 5 rows into the table.

There are a couple of things to keep in mind about this method.

  • You can insert up to 1,000 records as part of an INSERT statement using this method. If you need to insert more, consider using multiple INSERT statements.
  • Also, on MySQL, the maximum size of the entire INSERT statement must be smaller than the database parameter “max_allowed_packet“, which is the number of bytes. This can be changed (I’ll write about this in another article).

 

SQL Insert Multiple Rows for Oracle

The above method for inserting multiple rows in SQL won’t work for Oracle. But here’s a method that does work for Oracle. It’s slightly different but it gets the job done.

INSERT ALL
INTO customer (first_name, last_name) VALUES ('Kristen', 'Rowley')
INTO customer (first_name, last_name) VALUES ('Jed', 'Tomlinson')
INTO customer (first_name, last_name) VALUES ('Margie', 'Escobar')
INTO customer (first_name, last_name) VALUES ('Harriette', 'Mejia')
INTO customer (first_name, last_name) VALUES ('Francis', 'Little')
SELECT * FROM dual;

This syntax is Oracle-specific as the other version won’t work in Oracle.

There are a few things to notice here:

  • The INSERT ALL keyword is used to instruct the database to insert all records.
  • The INTO, table name, column names, and VALUES keyword are repeated for every row. This is quite a bit of code repetition but needs to be done.
  • There is no comma after each of the INTO lines. I often forget this and get syntax errors. So, don’t add a comma.
  • We need to add SELECT * FROM dual at the end. The INSERT ALL statement requires a SELECT statement, and because we’re just inserting values we specify, we don’t need any data from a table. So we select from the “dual” table to get the statement to work.
  • If you add use a sequence and call the nextval value, then the same row will be used for each inserted row.

 

SQL Insert from Another Table

If you have your data in another table and want to insert it into a new table, you can use an INSERT statement and a SELECT statement. This works in all database vendors.

INSERT INTO customer (first_name, last_name)
SELECT fname, lname
FROM list_of_customers
WHERE active = 1;

This example will insert data into the customers table from data in the list_of_customers table where the value of active is 1.

There is no limit to the number of rows that can be inserted here.

 

Conclusion

So that’s how you can insert multiple rows in SQL. Most databases follow the same syntax, except Oracle who requires it in a different format.

9 thoughts on “How to Insert Multiple Rows in SQL”

  1. Frank Hines Jr.

    I work with Oracle and your example of inserting multiple records into that database is extremely helpful.

    Thank you!

  2. Oops :-)
    Long, long time a go a faced with an old database version (Sybase 12.5) and I solved using these trick:
    INSERT INTO customer (first_name, last_name)
    select ‘Kristen’, ‘Rowley’ union all
    select ‘Jed’, ‘Tomlinson’ union all
    select ‘Margie’, ‘Escobar’ union all
    select ‘Harriette’, ‘Mejia’ union all
    select ‘Francis’, ‘Little’;
    Sybase suports select without FROM.

    In Oracle:
    INSERT into customer (first_name, last_name)
    select ‘Kristen’, ‘Rowley’ from dual union all
    select ‘Jed’, ‘Tomlinson’ from dual union all
    select ‘Margie’, ‘Escobar’ from dual union all
    select ‘Harriette’, ‘Mejia’ from dual union all
    select ‘Francis’, ‘Little’ from dual;
    If you are facing an old version database without multiple insert feature, these workaround works fine.

  3. Here is my code, but I get an error when I try running it. What might be the issue?

    INSERT ALL
    INTO Faculty (FID, Lastname, Firstname, Email, Hiredate) VALUES (1,’Andrew’,’Rice’,’[email protected]’,2019-03-14)

    INTO Faculty (FID, Lastname, Firstname, Email, Hiredate) VALUES (2,’Isabella’,’Emma’,’[email protected]’,2020-08-20)

    INTO Faculty (FID, Lastname, Firstname, Email, Hiredate) VALUES (3,’Olivia’,’Wyatt’,’[email protected]’,2021-09-15)

    SELECT * FROM dual;

    1. Hi JJ, what error are you getting? The only issue that I can see is that the date values should be enclosed in single quotes, or perhaps using a function (depending on which database you’re using).

  4. INSERT ALL
    INTO userlogs(full_name,password,phone_number,username)VALUES(‘Tabitha Okeke’,’Flare1′,’09073738316′,’tab.com’)
    INTO userlogs(full_name,password,phone_number,username)VALUES(‘Chedom Okafor’,’flAre1′,’09073458316′,’che.com’)
    INTO userlogs(full_name,password,phone_number,username)VALUES(‘Chibuike Okafor’,’FLare1′,’09073778316′,’chi.com’)
    INTO userlogs(full_name,password,phone_number,username)VALUES(‘Bullion Chukwu’,’Zonals1′,’09071238316′,’[email protected]’)
    INTO userlogs(full_name,password,phone_number,username)VALUES(‘Victor Okafor’,’FlareS1′,’09070968316′,’victor.com’)
    INTO userlogs(full_name,password,phone_number,username)VALUES(‘Chisom Okafor’,’Flare2′,’09073734516′,’chisom.com’)
    INTO userlogs(full_name,password,phone_number,username)VALUES(‘Somto Okafor’,’Flares3′,’09073745316′,’som.com’)
    INTO userlogs(full_name,password,phone_number,username)VALUES(‘Vivian Okafor’,’FlarE1′,’09077848316′,’vee.com’)
    INTO userlogs(full_name,password,phone_number,username)VALUES(‘Mary Okafor’,’FlaReS1′,’09073734316′,’[email protected]’)
    INTO userlogs(full_name,password,phone_number,username)VALUES(‘Joy Okafor’,’FlaRe156′,’09073789316′,’[email protected]’)
    SELECT * FROM dual;

    This my code but I’m getting constraint violation error

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Table of Contents