Home >Database >Mysql Tutorial >Can a Single SQL Query Insert Multiple Rows?
Question:
When inserting multiple pieces of data into a database table, executing INSERT statements one by one is inefficient and cumbersome. Can I use a single SQL statement to insert multiple rows of data at the same time?
Question:
Can I use one SQL statement to insert four rows of data into the target table at the same time?
Answer:
Yes. In SQL Server 2008 and later versions, you can insert multiple rows of data using a single INSERT statement.
Solution:
Use the following syntax to insert multiple rows of data through a single SQL statement:
<code class="language-sql">INSERT INTO MyTable (Column1, Column2, Column3) VALUES (Value1, Value2, Value3), (Value1, Value2, Value3), ...</code>
For example, insert four rows of data into a table named MyTable. The table contains three fields: Person, Id and Office. The SQL statement is as follows:
<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>
The above is the detailed content of Can a Single SQL Query Insert Multiple Rows?. For more information, please follow other related articles on the PHP Chinese website!