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:
- UNPIVOT to Select Columns as Rows
- Flatten Hierarchical Data using a Single SQL
- LISTAGG to Convert Rows to Comma-Separated String
{ 9 comments… read them below or add one }
What is the use of “select * from dual” here?
Thanks..
really thanks you
before i didnt thinking use ‘dual’
pls tell me the use of dual here?
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.
Thank you. i got my answer from here
Thank you,got my answer from here.
Thank you, this article also helped me.
Helpful post! Helped me write inserts of bulk data in simple way. Thank you!!