OnlineTextLab
Esc

CSV to SQL INSERT Generator

Turn a CSV into INSERT statements escaped for your database, with an optional CREATE TABLE and batching.

SQL output

Your SQL appears here as you type.

Nothing to paste yet? Use Try an example to see it work.

Options
Use schema.table for a schema.
SQL dialect
Rows per INSERT
Delimiter

Runs on your device. Your files and text are not uploaded.

About CSV to SQL Inserts

Loading a spreadsheet into a database by hand is where names like O'Brien break a script, or where a postcode of 02134 quietly becomes 2134. This generator writes the statements with values escaped for the dialect you pick, and keeps a column as text when any value in it needs to be.

It is built for the common small job: seeding a development database, loading a lookup table, or sending a support engineer a script they can review before running. For millions of rows, your database's own bulk loader will be much faster.

You get a SQL script to copy or download. Since it is plain text, you can read every statement before it touches your data.

How it works

Single quotes in values are doubled in every dialect. MySQL also treats a backslash as an escape by default, so backslashes are doubled there and left alone elsewhere. SQL Server has no boolean literal, so true becomes 1, and text with non-ASCII characters gets the N'...' prefix to keep it from being mangled. NUL characters are removed, since databases generally refuse them.

A cell becomes an unquoted number only when it is a plain number with no leading zero, and booleans become TRUE or FALSE. If any cell in a column is text, the entire column is quoted, so 02134 and 10001 are written alike. Empty cells become NULL unless you turn that off.

Column and table names are left bare when they are safe and quoted for the dialect otherwise: double quotes in PostgreSQL, backticks in MySQL, and square brackets in SQL Server. This matters for reserved words and names with spaces, and it also means no value in your file can inject SQL through a header.

How to use CSV to SQL Inserts

  1. Paste CSV with a header row, or use Open file.
  2. Set the Table name and SQL dialect, which sit open at the top of Options.
  3. Choose Rows per INSERT, and turn on Include CREATE TABLE if the table does not exist yet.
  4. Review the statements, then use Copy or Download .sql.

An apostrophe, a leading zero and an empty cell, for PostgreSQL

Input
id,name,postcode,active
1,Conor O'Brien,02134,true
2,Ada Lovelace,10001,
Output
INSERT INTO customers (id, name, postcode, active) VALUES (1, 'Conor O''Brien', '02134', TRUE);
INSERT INTO customers (id, name, postcode, active) VALUES (2, 'Ada Lovelace', '10001', NULL);

The apostrophe in O'Brien is doubled and postcode is quoted throughout, because 02134 must not become a number. The empty active cell is NULL, and name needs no identifier quotes as it is not reserved in this list.

When it helps

  • Seeding a local or staging database from a spreadsheet of test data.
  • Loading a small reference table, such as tax rates or country codes, in a migration.
  • Giving an operations colleague a script they can review before running.
  • Moving a few hundred rows between systems when no direct connection is available.

Tips for a better result

  • Use 100 Rows per INSERT for faster loading. SQL Server allows at most 1,000 rows in one VALUES list, so 1,000 is the top option here.
  • Wrap the script in a transaction when you run it, so a failure partway through leaves nothing half-loaded.
  • Check Include CREATE TABLE output before relying on it. Types are inferred from the rows you gave, so a short sample can give a DECIMAL or VARCHAR that is too small later.
  • In PostgreSQL, a quoted identifier keeps its case. A header of Name stays unquoted here and folds to lowercase name.

Good to know

  • Dates are written as quoted strings. Most databases convert ISO dates like 2024-03-01 automatically, but other formats may be rejected.
  • No primary keys, indexes or constraints are generated by Include CREATE TABLE.
  • A cell containing only the word null becomes NULL. Turn off Write numbers and booleans unquoted to keep it as text.
  • The list of reserved words covers common SQL keywords, not every word reserved by each database, so use a different column name if your database complains.

Questions people ask

Is the output safe from SQL injection?

Values and identifiers are escaped for the dialect you selected, so a cell such as '; DROP TABLE users; -- stays as text. Pick the right dialect, because escaping rules differ, notably for MySQL backslashes.

Which dialect should I choose for MariaDB or Amazon Redshift?

MySQL for MariaDB, since it shares the backslash behavior and backtick quoting. Redshift follows PostgreSQL closely. Standard works for most other databases.

Why was my numeric column quoted?

At least one value in that column is not a plain number, for example it has a leading zero or a unit. The whole column is quoted so that it has one type, and a numeric column in your database will still convert the values.

How many rows can this handle?

Up to the 10 MB file limit. A script from a file that size can load slowly as separate INSERTs, so use your database's COPY or LOAD DATA for large imports.

What to use next

Last reviewed . Read our editorial policy.