Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inserting multiple rows with one insert command

Tags:

sql

oracle

Is it possible to insert more than one row in a table with one insert statement? I know this will happen if I do:

insert into table ( fields ) select values from another_table

But what if I want to insert:

row 1 - ( a1, b1, c1 )
row 2 - ( a2, b2, c2 )
...
row n - ( an, bn, cn )

with just one insert command?

like image 292
trinity Avatar asked Nov 11 '10 06:11

trinity


People also ask

How can insert multi rows in only one insert statement?

INSERT-SELECT-UNION query to insert multiple records Thus, we can use INSERT-SELECT-UNION query to insert data into multiple rows of the table. The SQL UNION query helps to select all the data that has been enclosed by the SELECT query through the INSERT statement.

How do I insert multiple rows with the same primary key?

The whole idea of a primary key is to have a unique identifier for each row, so you can not do that. However, if you want a way of grouping rows, you can either add a group column to your table, or create a table for the grouping. For example group_members and have that contain two columns, "group_id" and "row_id".


1 Answers

Two solutions (source : http://appsfr.free.fr/spip.php?article21 ):

INSERT ALL
INTO table (column1, column2)
VALUES (value1, value2)
INTO table (column1, column2)
VALUES (value1, value2)
...etc...
SELECT * FROM DUAL ;

or

INSERT INTO table (column1, column2)
SELECT value1, value2 FROM DUAL UNION ALL
SELECT value1, value2 FROM DUAL UNION ALL
...etc...
SELECT value1, value2 FROM DUAL ;
like image 140
Loïc Février Avatar answered Sep 24 '22 20:09

Loïc Février