问题
I have say 100 rows data to insert in MySQL database table.
But i dont want to write all 100 INSERT
statements.
Is there any bulk insert Statement in SQL ???
Please help with code if possible.
回答1:
INSERT INTO tbl (col1, col2) VALUES ('val1', 'val2'), ('val3', 'val4'), ...
And please read documentation first next time
回答2:
As the MySQL manual states:
INSERT
statements that useVALUES
syntax can insert multiple rows. To do this, include multiple lists of column values, each enclosed within parentheses and separated by commas. Example:INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
Furthermore, from the section on speed of INSERT:
If you are inserting many rows from the same client at the same time, use INSERT statements with multiple
VALUES
lists to insert several rows at a time. This is considerably faster (many times faster in some cases) than using separate single-row INSERT statements. If you are adding data to a nonempty table, you can tune the bulk_insert_buffer_size variable to make data insertion even faster. See Section 5.1.3, “Server System Variables”.
来源:https://stackoverflow.com/questions/10429348/batch-insert-sql-statement