Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

SQL INSERT Comma Separated Values: Step-by-Step

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To insert comma-separated values into a SQL table, you write one INSERT INTO statement with a VALUES clause that lists each row in parentheses, separated by commas. For example: INSERT INTO users (name, email) VALUES ('Ann', 'ann@example.com'), ('Bob', 'bob@example.com'); — that single statement adds two rows. If your comma-separated data is a string like 'Ann,ann@example.com', you must first split it into columns (using your database's split function, a staging table, or your import tool) before inserting. This guide covers both cases and the mistakes that cause syntax errors or mangled data.

What You'll Need

  • A database client (psql, MySQL Workbench, SQL Server Management Studio, DBeaver, or similar) or a programming language with a database driver.
  • The exact table name and column names — run DESCRIBE table_name; (MySQL) or \d table_name (PostgreSQL) to confirm.
  • Permission to insert rows (INSERT privilege on the table).
  • Your comma-separated data in a text file or spreadsheet if you are importing more than a handful of rows.

Step-by-Step

  1. Confirm the table structure. List the columns and their data types. You need to know the order and which columns accept NULL or have defaults. For example, in MySQL: SHOW COLUMNS FROM users;. In PostgreSQL: \d users. In SQL Server: sp_help 'users';. Note any auto-increment primary key — you usually omit it from the insert.
  2. Write the INSERT with an explicit column list. Always name the columns: INSERT INTO users (name, email) VALUES .... Without the column list, the database expects values for every column in table order, which breaks when the schema changes.
  3. Add each row as a parenthesised tuple, separated by commas. Example: INSERT INTO users (name, email) VALUES ('Ann', 'ann@example.com'), ('Bob', 'bob@example.com'), ('Cara', 'cara@example.com');. The final tuple ends with a semicolon, not a comma.
  4. Quote text values with single quotes. Numbers go unquoted. Dates use the format your database expects — ISO '2024-05-01' works in MySQL, PostgreSQL, and SQL Server. Escape an apostrophe inside text by doubling it: 'O''Brien'.
  5. If your data is a single comma-separated string, split it first. For a one-off, split manually into columns. For bulk data, load the raw line into a staging table with one column, then split. MySQL: SUBSTRING_INDEX. PostgreSQL: split_part or string_to_array. SQL Server: STRING_SPLIT. Example in PostgreSQL: SELECT split_part(line, ',', 1) AS name, split_part(line, ',', 2) AS email FROM staging; then insert the result.
  6. For large files, use the native bulk loader instead of a giant INSERT. PostgreSQL: COPY users (name, email) FROM '/path/file.csv' WITH (FORMAT csv);. MySQL: LOAD DATA INFILE '/path/file.csv' INTO TABLE users FIELDS TERMINATED BY ',';. SQL Server: BULK INSERT users FROM 'C:\path\file.csv' WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\n');. These are far faster and handle quoting rules correctly.
  7. Run the statement and check the result. The client reports rows affected. Then verify: SELECT * FROM users WHERE email IN ('ann@example.com','bob@example.com');. If the count is wrong, roll back (if inside a transaction) or delete the inserted rows by a unique key before retrying.
  8. Wrap multi-row inserts in a transaction when supported. BEGIN; ... COMMIT; ensures all rows land or none do. This matters when a later tuple violates a constraint — without a transaction, earlier rows may already be committed.

Common Mistakes

  • Trailing comma before the semicolon. VALUES (1),(2),; is a syntax error. The last tuple has no comma after it.
  • Missing quotes around text. VALUES (Ann) is read as a column name, not a string.
  • Unescaped apostrophes. 'O'Brien' terminates the string early. Use 'O''Brien'.
  • Column count mismatch. Each tuple must supply exactly as many values as columns listed. A missing value or an extra comma shifts every following value into the wrong column.
  • Assuming commas inside quoted text are safe. In a CSV file, "Smith, John" is one field; a naive split on commas breaks it. Use a proper CSV parser or the database's bulk loader.
  • Inserting into an auto-increment column. Supplying a value for an identity column usually fails unless you enable identity insert (SQL Server: SET IDENTITY_INSERT) — and you rarely need to.

Tips

  • Test with two or three rows before running a thousand-row insert.
  • Use a transaction so a failure halfway through leaves the table unchanged.
  • For repeated imports, create a staging table with all columns as text, load the raw CSV, clean and cast the data with SQL, then insert into the final table. This isolates bad rows.
  • Keep a unique key (email, SKU, order ID) so you can find and remove rows from a failed run.
  • When generating SQL from a spreadsheet, use a formula that doubles apostrophes in text fields.

Troubleshooting

  • "Column count doesn't match value count" — count the columns in your list and the values in each tuple. They must be equal. Check for a stray comma inside a quoted string that your editor split visually.
  • "Syntax error near ','" — usually a trailing comma, a missing quote, or an unescaped apostrophe in the row before the error position.
  • "Duplicate entry" or unique constraint violation — one row already exists. Use INSERT IGNORE (MySQL), ON CONFLICT DO NOTHING (PostgreSQL), or MERGE (SQL Server) if skipping duplicates is intended; otherwise remove the offending row.
  • Data landed in the wrong columns — the tuple order did not match the column list, or a comma inside a value shifted fields. Delete the affected rows and re-insert with correct quoting.
  • Bulk load fails on file path or permissions — the database server, not your client, reads the file. The path must be on the server and readable by the database process. Use client-side import tools if you cannot place files on the server.
  • Character encoding errors — save the CSV as UTF-8 and specify the encoding in the load command if your database supports it.

Frequently Asked Questions

Can I insert multiple rows in one statement?

Yes. List each row as a parenthesised tuple separated by commas. This is standard in MySQL, PostgreSQL, SQL Server, and SQLite.

How do I insert a value that contains a comma?

Put the whole value in single quotes: 'Smith, John'. The comma inside quotes is treated as text, not a separator.

What is the difference between INSERT and bulk load?

INSERT is a SQL statement for a handful to a few thousand rows. Bulk loaders (COPY, LOAD DATA, BULK INSERT) read a file directly and are much faster for large datasets.

Do I need to list column names?

It is strongly recommended. Without the list, values must match every column in table order, and the statement breaks if the schema changes.

How do I insert only some columns?

Name just those columns in the column list and supply values only for them. Omitted columns take their default value or NULL.

Can I undo an INSERT?

Only if it ran inside a transaction that has not been committed — then ROLLBACK. After commit, you must delete the rows explicitly, ideally using a unique key.