How to Generate SQL INSERT Scripts from a CSV File

How to Generate SQL INSERT Scripts from a CSV File - Rons Data Merge


Someone sends you a spreadsheet of 400 rows and asks for it "in the database by Friday". You could type 400 INSERT statements, write a small program, or use a command-line converter. Or you can let a template do it, look at the result row by row, and keep the template for next time.

Rons Data Merge has two SQL merge types:

  • SQL Insert – one script with an INSERT statement for each row
  • SQL Merge – one script that loads the rows and runs a SQL MERGE against a table you name, so existing rows are updated and new rows are added

Both are generic SQL scripts, not written for one particular database. Rons Data Merge only writes the script; it does not connect to a database. You run the script in your own database tool, where you can read it first and adjust it if your database needs a different syntax.

1. Load the Table

Create a new merge and open the CSV file (or TSV, PSV, TAB or TXT), or paste a table copied from Excel. Check the grid:

  • The column names become the column names in your script, so make them match the database table. Rename them in the grid if needed.
  • Fix any obvious problems: empty rows, a stray total line at the bottom, a heading repeated halfway down.

2. Choose SQL Insert

Choose the SQL Insert merge type. It comes with a sample template that already loops over the columns, so it works with any table. A simplified version (without the apostrophe handling described below) looks like this:

INSERT INTO output
(
{{ for column in columns }}	{{ column.code }}{{ if !for.last }}, {{ end }}
{{ end }}
)
VALUES
(
{{ for column in columns }}	'{{ row[column.code] }}'{{ if !for.last }},{{ end }}
{{ end }}
);

Because the template loops over columns, a column added to the CSV appears in the script without editing the template.

3. Adjust the Template to Your Table

Typical changes:

  • Table name: replace output with your table name.
  • Column names: the loop uses each column's code (the column name in lower case with underscores, for example First Name becomes first_name). If your database uses different names, rename the columns in the grid, or list the columns by hand in the template.
  • Numbers and dates: the sample puts every value in quotes. For numeric columns you may want the value without quotes, and dates in the format your database expects.
  • Apostrophes: a name such as O'Brien contains a quote. The SQL Insert output escapes apostrophes, so values like this do not break the statement. If you write your own template from scratch, check a row with an apostrophe in the preview.
  • Header and footer: SQL Insert writes one script, so use the header for anything that runs once at the top (for example BEGIN TRANSACTION; or a USE statement) and the footer for anything at the end (COMMIT;).

4. Preview the Script

Step through the rows with first, previous, next and last, and read the statement each row produces. Look at the awkward ones: empty cells, very long text, values with quotes or commas. Preview is not limited, even in the free Lite edition.

5. Write the Script

Choose the output file and press Start. Open the script in your database tool, run it against a test database first, and keep the .merge file so the same job is one click next time.

When to Use SQL Merge Instead

Use SQL Merge when the table already holds some of the rows and you want to update them as well as add new ones. Name the target table, and make sure the CSV has the key column the MERGE matches on. The script is generic SQL, and not every database writes MERGE the same way, so read the script and adjust the template for your database before you run it.

Other Ways to Do It

There are good command-line tools for this job, such as csvsql from the open-source csvkit, which can generate INSERT statements or load a CSV straight into a database. They suit people who script everything. Rons Data Merge suits people who want to see the table, edit the statement as text, preview each row and save the job as a project.

Lite and Pro

The free Lite edition has every feature, including both SQL types, with no time limit. It runs merges of up to 250 rows (loading and preview are not limited) and shows an advertising banner; in a business it is for evaluation. Rons Data Merge Pro removes the row limit and is a one-time purchase of about US$17.50. Your data stays on your computer: Rons Data Merge writes a file, and you decide where it goes.

See Also