Richard C.
—If you want to insert many rows into a SQL table, you have to repeat INSERT INTO over and over in separate statements.
INSERT INTO Person VALUES (1, "Amir"); INSERT INTO Person VALUES (2, "Sofia"); INSERT INTO Person VALUES (3, "Aya"); ...
Is there a way to use a single statement and avoid repetition?
Assume the table we are working with has just Id
and Name
columns. This code works on SQL Server, MySQL, and PostgreSQL.
CREATE TABLE Person ( Id INT PRIMARY KEY, Name VARCHAR(255) );
The solution depends on which database server and version you are using. They each support different syntax.
SQL Server allows you to enter up to 1000 rows in a single statement.
INSERT INTO Person (Id, Name) VALUES (1, 'Amir' ), (2, 'Sofia'), (3, 'Aya'), (4, 'Mateo'), (5, 'Leila'), (6, 'Yara'), (7, 'Ndidi'), (8, 'Santiago');
After 1000 entries, performance degrades, so Microsoft wants you to split your entries into separate transactions.
If you’re using an earlier version of SQL Server, you can’t use this syntax. You have to use multiple INSERT statements.
Some answers online suggest using UNION ALL syntax to create a SELECT statement from which you INSERT. We don’t recommend this for performance reasons.
INSERT INTO Person (Id, Name) SELECT 1, 'Amir' UNION ALL SELECT 2, 'Sofia' UNION ALL SELECT 3, 'Aya';
Rather investigate the Microsoft BULK INSERT tool for large inserts.
BULK INSERT Person FROM '\\data\people.dat';
MySQL and PostgreSQL also support modern syntax. This has been available since around 2006.
INSERT INTO Person (Id, Name) VALUES (1, 'Amir' ), (2, 'Sofia'), (3, 'Aya'), (4, 'Mateo'), (5, 'Leila'), (6, 'Yara'), (7, 'Ndidi'), (8, 'Santiago');
(Note that if you are trying this in SQLFiddle.com for MySQL, you need to enter this code in the schema panel, not the query panel).
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at [email protected].
If you are a California resident, see our Supplemental notice.