Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to do multi-row insert in Oracle?

I'm looking for a good way to perform multi-row inserts into an Oracle 9 database. The following works in MySQL but doesn't seem to be supported in Oracle.

INSERT INTO TMP_DIM_EXCH_RT  (EXCH_WH_KEY,   EXCH_NAT_KEY,   EXCH_DATE, EXCH_RATE,   FROM_CURCY_CD,   TO_CURCY_CD,   EXCH_EFF_DATE,   EXCH_EFF_END_DATE,   EXCH_LAST_UPDATED_DATE)  VALUES     (1, 1, '28-AUG-2008', 109.49, 'USD', 'JPY', '28-AUG-2008', '28-AUG-2008', '28-AUG-2008'),     (2, 1, '28-AUG-2008', .54, 'USD', 'GBP', '28-AUG-2008', '28-AUG-2008', '28-AUG-2008'),     (3, 1, '28-AUG-2008', 1.05, 'USD', 'CAD', '28-AUG-2008', '28-AUG-2008', '28-AUG-2008'),     (4, 1, '28-AUG-2008', .68, 'USD', 'EUR', '28-AUG-2008', '28-AUG-2008', '28-AUG-2008'),     (5, 1, '28-AUG-2008', 1.16, 'USD', 'AUD', '28-AUG-2008', '28-AUG-2008', '28-AUG-2008'),     (6, 1, '28-AUG-2008', 7.81, 'USD', 'HKD', '28-AUG-2008', '28-AUG-2008', '28-AUG-2008'); 
like image 760
jamz Avatar asked Sep 02 '08 13:09

jamz


People also ask

How can I insert more than 1000 rows in Oracle?

The 1000 limit only applies when you are passing in the rows using a values statement - if you were inserting based on a select from a table then there is no limit. The row constructor, using VALUES, has a limit of up to 1000 rows. You can split the insert in two chuncks, or you can use SELECT ... UNION ALL instead.

How do I insert multiple rows at a time in SQL?

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.


1 Answers

In Oracle, to insert multiple rows into table t with columns col1, col2 and col3 you can use the following syntax:

INSERT ALL    INTO t (col1, col2, col3) VALUES ('val1_1', 'val1_2', 'val1_3')    INTO t (col1, col2, col3) VALUES ('val2_1', 'val2_2', 'val2_3')    INTO t (col1, col2, col3) VALUES ('val3_1', 'val3_2', 'val3_3')    .    .    . SELECT 1 FROM DUAL; 
like image 153
Myto Avatar answered Oct 24 '22 16:10

Myto