Home >Database >Mysql Tutorial >Can SQL Insert Multiple Rows at Once?
Can SQL insert multiple rows of data at one time?
When inserting multiple sets of data into a SQL table with multiple columns, writing separate INSERT statements for each row can be tedious. Fortunately, SQL Server 2008 provides a more efficient solution.
Multi-line INSERT statement
SQL Server 2008 supports inserting multiple rows into a table using a single INSERT statement. This can be accomplished by specifying multiple sets of values within parentheses, separated by commas.
Grammar:
<code class="language-sql">INSERT INTO MyTable (Column1, Column2, Column3) VALUES (Value1, Value2, Value3), (Value4, Value5, Value6)</code>
Example:
Suppose there is a table named "MyTable" with three columns: "Person", "Id" and "Office". To insert four rows of data, you can use the following statement:
<code class="language-sql">INSERT INTO MyTable (Person, Id, Office) VALUES ('John', 123, 'Lloyds Office'), ('Jane', 124, 'Lloyds Office'), ('Billy', 125, 'London Office'), ('Miranda', 126, 'Bristol Office');</code>
By taking advantage of this multi-row INSERT feature, you can simplify data insertion operations and improve the efficiency of your SQL queries.
The above is the detailed content of Can SQL Insert Multiple Rows at Once?. For more information, please follow other related articles on the PHP Chinese website!