You can build SQL INSERT statements from Excel by concatenating the columns into a text formula, then pasting the result into your database tool. Put the table name and column list in the formula, wrap text values in single quotes, leave numbers bare, save the output as a .sql file, and run it with a database client or the command line. The approach works for MySQL, PostgreSQL, SQL Server, SQLite and most other relational databases, with small differences in quoting and date formats described below.
What You'll Need
- Excel (or a comparable spreadsheet such as LibreOffice Calc or Google Sheets) with your data laid out in columns.
- Access to the target database, including a user account with INSERT permission on the table.
- A database client such as MySQL Workbench, pgAdmin, DBeaver, SQLiteStudio or the vendor's command-line tool.
- A text editor that handles plain text, for saving and reviewing the generated statements.
- Awareness that special characters, apostrophes in names, and dates are the usual sources of errors.
Step-by-Step
- Prepare the spreadsheet first. Put one column per database column and one row per record, starting in row 1 with header names that match your table's column names exactly. Delete blank rows, stray totals and merged cells. A clean grid is the single biggest factor in a first-time success.
- Check data types against the table. Look at your table definition and confirm which columns are text, which are numeric, and which are dates. Text values must be wrapped in single quotes; numbers must not be. Mixing them up is the most common reason a generated script fails.
- Add a helper column for the statement. In the first empty column beside your data, write a formula that assembles a single INSERT. In Excel, if column A holds the value row for a text column and column B holds a number, the formula roughly follows this pattern: start with the literal text INSERT INTO your_table (your_column, other_column) VALUES ( then concatenate a single quote, the cell in A, a single quote, a comma, the cell in B, and a closing parenthesis with a semicolon. The exact text varies by database, so build one row and test it before filling down.
- Wrap text values safely. Use CHAR(39) in Excel instead of typing apostrophes directly into the formula when you can, or type apostrophes carefully around cell references. Reference cell contents rather than hard-coding them whenever you can so a single formula repeat handles all rows.
- Fill the formula down and copy the results. Drag the fill handle to the last data row, then copy the new column and paste it into a plain text editor. Pasting straight into some database tools can bring formatting along, so pasting into a text editor first and saving as a .sql file is safer.
- Test with one row only. Before running hundreds of statements, delete everything except the first INSERT and run it. If it succeeds, the rest will almost certainly succeed too. If it fails, you have one statement to debug instead of a whole script.
- Run the script. In MySQL Workbench or DBeaver, open the .sql file and execute it; in pgAdmin, use the Query Tool and run the file; on the command line, use the tool's file input option, such as redirecting a file into sqlite3. Many clients also let you run statements one at a time, which makes errors easier to trace.
- Verify the import. Run a SELECT count and spot-check a few rows against the spreadsheet, particularly the row with the apostrophe-heavy name, the row with the date, and the last row. Then commit if your client uses manual commit, or roll back and fix if the counts do not match.
Common Mistakes
Unescaped apostrophes. Names such as O'Brien break a statement because the apostrophe closes the text value early. Replace a single apostrophe inside a value with two apostrophes in SQL Server and PostgreSQL and with a backslash-escaped apostrophe in MySQL, or escape according to your engine's rules. Some tools offer a setting for this; if you are unsure, test one row first.
Numbers stored as text. Excel silently keeps leading zeros and formats that look numeric as text. Confirm with the table definition and remove quote marks only from genuine numeric columns; adding quotes around a number often still works in some engines but fails in others.
Wrong date format. Excel dates may be stored as serial numbers, and locale formatting can turn 03/04/2026 into an ambiguous value. Pass dates in the format your database expects, such as YYYY-MM-DD, and wrap only the final string in quotes.
Missing column list. A statement without the parenthesised column list depends on the table's exact column order. If the table order changes, values land in the wrong fields with no error. Always list the columns explicitly.
Oversized single statements. Concatenating thousands of rows into one value list can hit query length limits. Keep one INSERT per row, or batch a few dozen at a time, rather than one enormous statement.
Tips
Keep a copy of the spreadsheet data as CSV before you start; if the generated script goes wrong, you can regenerate from the saved copy quickly. Generate the statements in a column that you can delete afterwards so the source data stays untouched. If the data is large or recurring, an import wizard built into many database tools (or a bulk-load feature) is faster and more reliable than string formulas, and it is the better long-term approach once you have done this a few times. Always run a test insert against a staging or development database before touching production data, and take a backup first. Save the working formula in a notes file so the next import takes minutes instead of starting from scratch.
Troubleshooting
Syntax error near the first value. The usual causes are an unbalanced single quote, a missing comma, or a text value wrapped without quotes. Copy the failing statement and count quotation marks in pairs.
Incorrect integer, date or decimal value. A quote is around a number, or the date format does not match the engine. Fix the formula so numbers stay bare and dates are formatted explicitly.
Duplicate key error. Your table has a primary key or unique index and a row already exists. Decide whether to skip existing rows, update them, or clear the target table first. Clearing data deletes it permanently, so back up before doing so.
Value too long or data truncated. The spreadsheet value exceeds the column's defined length or type. Shorten the data or widen the column, which is a schema change and should be reviewed.
Permissions error. The account may have read access but not INSERT rights, or may not be allowed to write to that table. Ask the database administrator to confirm the account's permissions.
Statements run but nothing appears. If your client is in manual-commit mode, the transaction may not be saved. Commit it, or rerun against a fresh session and check the row count.
Frequently Asked Questions
Can I do this without a database client?
Yes. Generate the statements in the spreadsheet, save them as a .sql file, and run that file through your engine's command-line tool. The client just makes errors easier to see.
Does this work for any database?
The pattern is the same, but quoting and escaping rules differ between engines. Check your engine's documentation for apostrophe handling and date formats before generating the full script.
How do I handle apostrophes in names?
Double the apostrophe for SQL Server and PostgreSQL, or use the escape character for MySQL. Always test one row containing an apostrophe before running the rest.
What if my spreadsheet has empty cells?
An empty cell produces an empty quoted string or a blank value, depending on the formula. Decide whether blanks should become NULL or an empty string, and adjust the formula accordingly.