INSERT ALL: Insert Multiple Rows with a Single INSERT Statement

September 18, 2012

in keywords, sql

Insert All

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

{ 6 comments… read them below or add one }

Ramesh September 30, 2012 at 2:25 pm

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

KiranGayathri February 21, 2013 at 6:14 pm

Thanks..

choe March 25, 2013 at 7:33 am

really thanks you
before i didnt thinking use ‘dual’

bandna January 18, 2014 at 10:17 pm

pls tell me the use of dual here?

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.

Arindam March 25, 2014 at 7:38 am

Thank you. i got my answer from here

Leave a Comment

Previous post:

Next post: