5

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?

5 Answers 5

7

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 ;
Sign up to request clarification or add additional context in comments.

Comments

5

Insert All

INSERT ALL
   INTO mytable (column1, column2, column3) VALUES ('val1.1', 'val1.2', 'val1.3')
   INTO mytable (column1, column2, column3) VALUES ('val2.1', 'val2.2', 'val2.3')
   INTO mytable (column1, column2, column3) VALUES ('val3.1', 'val3.2', 'val3.3')
SELECT * FROM dual;

Comments

3
INSERT INTO products (product_no, name, price) VALUES
    (1, 'Cheese', 9.99),
    (2, 'Bread', 1.99),
    (3, 'Milk', 2.99);

2 Comments

The question is for oracle so why are you posting a PostgreSQL answer?
I removed 'PostgreSQL' in my answer. For your info, this is standard SQL-92.
0
INSERT INTO College (CustomerID, FirstName, MiddleName, LastName)

SELECT 7, N'Charles', N'Simmons', N'Burns'
UNION ALL
SELECT 9, N'Dominic', N'Fred', N'Einsten'
UNION ALL
SELECT 12, N'Dave', N'William, N'Bryan';

NOTE: Letter N before each hard coded string value converts string to an NVARCHAR value to match the datatype of the column.

Comments

-5

No, this is not possible. As you already stated yourself, it is only possible with a select clause providing the insert values and rows.

1 Comment

at least after reading the accepted answer along with some other given, you definitely know now that it is possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.