INSERT ALL: Insert Multiple Rows with a Single INSERT Statement

September 18, 2012

in Keywords, SQL

INSERT ALL: Insert Multiple Rows with a Single INSERT Statement

A quick tip to get rid of the plodding way of inserting data into a table with multiple INSERT statements. Know what I mean? Say you have a table COLORS with this structure:

 Name             Type
 ---------------- ------------
 NAME             VARCHAR2(30)
 CATEGORY         VARCHAR2(10) 

And you want to create this data in the table COLORS:

NAME                           CATEGORY
------------------------------ --------
yellow                         1
red                            1
blue                           1
yellow                         2
blue                           2

Hands up all who write insert statements this way:

insert into colors (name, category)
values ('yellow', 1);

insert into colors (name, category)
values ('red', 1);

insert into colors (name, category)
values ('blue', 1);

insert into colors (name, category)
values ('yellow', 2);

insert into colors (name, category)
values ('blue', 2);

Good news for you — you can condense multiple INSERTS into a single SQL command with INSERT ALL.

INSERT ALL – the shorthand multi-table INSERT

This is how you’d do it:

insert all
into colors(name, category) values('yellow', 1)
into colors(name, category) values('red', 1)
into colors(name, category) values('blue', 1)
into colors(name, category) values('yellow', 2)
into colors(name, category) values('blue', 2)
select * from dual;

INSERT and INSERT ALL in action

A demo run to show identical results with the two styles:

Without INSERT ALL:

SQL> -- Without INSERT ALL
SQL> insert into colors (name, category)
  2  values ('yellow', 1);

1 row created.

SQL> insert into colors (name, category)
  2  values ('red', 1);

1 row created.

SQL> insert into colors (name, category)
  2  values ('blue', 1);

1 row created.

SQL> insert into colors (name, category)
  2  values ('yellow', 2);

1 row created.

SQL> insert into colors (name, category)
  2  values ('blue', 2);

1 row created.

SQL> select * from colors;

NAME                           CATEGORY
------------------------------ ----------
yellow                         1
red                            1
blue                           1
yellow                         2
blue                           2

With INSERT ALL:

SQL> -- With INSERT ALL
SQL> insert all
  2  into colors(name, category) values('yellow', 1)
  3  into colors(name, category) values('red', 1)
  4  into colors(name, category) values('blue', 1)
  5  into colors(name, category) values('yellow', 2)
  6  into colors(name, category) values('blue', 2)
  7  select * from dual;

5 rows created.

SQL> select * from colors;

NAME                           CATEGORY
------------------------------ ----------
yellow                         1
red                            1
blue                           1
yellow                         2
blue                           2

For Further Reading

A few similar DML constructs that make processing simpler:

{ 9 comments… read them below or add one }

1 Ramesh September 30, 2012 at 2:25 pm

What is the use of “select * from dual” here?

2 KiranGayathri February 21, 2013 at 6:14 pm

Thanks..

3 choe March 25, 2013 at 7:33 am

really thanks you
before i didnt thinking use ‘dual’

4 bandna January 18, 2014 at 10:17 pm

pls tell me the use of dual here?

5 oratabler January 19, 2014 at 12:42 am

A subquery is mandatory as per the INSERT ALL syntax: INSERT ALL (…) VALUES (…) does not work.

Insert is executed for each row returned by the subquery. “SELECT * FROM dual” returns a single row, so the INSERT clauses are executed once, which is useful when a hardcoded set of values is being inserted as in the example in this article.

6 Arindam March 25, 2014 at 7:38 am

Thank you. i got my answer from here

7 Sybil September 21, 2016 at 4:04 am

Thank you,got my answer from here.

8 iMax November 13, 2018 at 9:17 pm

Thank you, this article also helped me.

9 Samir March 10, 2019 at 10:48 pm

Helpful post! Helped me write inserts of bulk data in simple way. Thank you!!

Leave a Comment

Previous post:

Next post: