Home >Database >Mysql Tutorial >Can a Single SQL Query Insert Multiple Rows?

Can a Single SQL Query Insert Multiple Rows?

Susan Sarandon
Susan SarandonOriginal
2025-01-22 11:01:10788browse

Can a Single SQL Query Insert Multiple Rows?

Efficiently insert multiple rows of data: batch insertion with a single SQL statement

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn