Home >Database >Mysql Tutorial >How Can I Insert Multiple Rows into a SQL Table with a Single Query?

How Can I Insert Multiple Rows into a SQL Table with a Single Query?

Susan Sarandon
Susan SarandonOriginal
2025-01-22 11:21:11659browse

How Can I Insert Multiple Rows into a SQL Table with a Single Query?

Efficiently Inserting Multiple Rows into SQL Tables

SQL database management often involves adding data. For large datasets, inserting multiple rows individually using separate INSERT statements is inefficient. Fortunately, SQL offers a streamlined method to insert multiple rows with a single query, significantly boosting performance. This technique is particularly beneficial when dealing with bulk data insertion.

This approach, supported by SQL Server 2008 and later versions, uses a concise syntax:

<code class="language-sql">INSERT INTO MyTable (Column1, Column2, Column3) VALUES
(Value1, Value2, Value3), (Value1, Value2, Value3), ...</code>

Let's illustrate with an example. Suppose we have a table MyTable with columns Person, Id, and Office. To insert four rows, we can use the following single INSERT 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>

This single query accomplishes the same task as four individual INSERT statements, minimizing code complexity and improving database write efficiency. This method is a crucial technique for handling large-scale data insertion in SQL databases.

The above is the detailed content of How Can I Insert Multiple Rows into a SQL Table with a Single Query?. 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