You cannot run a SQL INSERT statement inside Excel itself, but you can build one in a cell so it can be pasted into a database tool. The fastest way is a formula that joins fixed text and cell values: ="INSERT INTO tablename (col1, col2) VALUES ('"&A2&"', '"&B2&"');". You then fill the formula down the column, copy the results, and paste them as plain text into your SQL client. This is handy when you have a few dozen or a few thousand rows of data in a spreadsheet and need to move them into a database without a proper import tool.
Note that this approach produces a text script, not a live connection. Excel never talks to the database; you are just using Excel as a very good string-building tool. If you have a real connection (Power Query, a linked table, an Access database), using that is safer than generating SQL by hand, because it handles escaping and data types for you.
What You'll Need
- Excel (desktop version on Windows or Mac works best; the formula below also works in Excel for the web)
- A worksheet with your data laid out in rows, one row per database record, with column headers on the first row
- The exact table name and column names from your database, spelled the way the database spells them
- A SQL client or database admin tool where you will paste the generated text, such as SQL Server Management Studio, MySQL Workbench, pgAdmin, DBeaver or a web-based admin panel
- Basic knowledge of what data types your target columns expect (text, numbers, dates, nulls)
Step-by-Step
- Lay out your data cleanly. Put each record on its own row. Keep columns in the same order as the columns you will insert into. Remove blank rows in the middle of the data and make sure no column header has stray spaces. Delete merged cells and any subtotal rows, because they break the pattern.
- Check your column names first. In your database tool, look up the real column names. A mistake like
customer_nameversusCustomerNamematters in some databases and not others, so copying the exact spelling saves a failed run. Also check whether the table has mandatory columns (NOT NULL with no default) that you must include. - Pick an empty helper cell in the first data row (row 2 if row 1 is headers). Click it and type an equals sign to start a formula, then build the statement around your cell references.
- Write the formula using ampersands to join text and cells. A working pattern for a two-column table looks like this:
="INSERT INTO customers (name, city) VALUES ('"&A2&"', '"&B2&"');". Here,A2is the name andB2is the city. The single quotes around each cell reference wrap the value as SQL text, and the semicolon ends the statement. - Press Enter and read the result. You should see something like
INSERT INTO customers (name, city) VALUES ('Acme Ltd', 'Leeds');in the cell. If you see a#NAME?error, one of your quotes is a straight quote that Excel turned into a curly quote, or the ampersands are in the wrong place. Retype the formula in a plain-text editor first if your keyboard auto-corrects quotes. - Fill the formula down. Select the cell, grab the small square at the bottom-right corner and drag down to the last row, or double-click the square to fill down automatically. Each row now produces its own statement with its own values.
- Handle numbers separately. SQL needs unquoted numbers:
INSERT INTO stock (item, qty) VALUES ('Bolt', 25);works, but'25'may fail or be stored as text in a strict database. For numeric columns, leave out the surrounding quotes:="INSERT INTO stock (item, qty) VALUES ('"&A2&"', "&B2&");". Convert the column to a real number format before this step so Excel does not add decimals or thousands separators. - Handle blanks and NULLs. An empty cell produces an empty string
'', which is not the same asNULL. If the column allows NULL, wrap it:IF(B2="","NULL","'"&B2&"'")so the statement readsNULLinstead of''. - Deal with apostrophes in your data. A name like
O'Brienbreaks the statement because the apostrophe ends the string early. The standard SQL fix is to double it:O''Brien. In Excel, useSUBSTITUTE(A2,"'","''")in place of the plain cell reference. Do this for every text column, or you will hit errors as soon as a real name arrives. - Format dates the way your database expects. Excel stores dates as serial numbers and shows them in your locale format. A date like
03/04/2026may be read differently by the database. UseTEXT(A2,"yyyy-mm-dd")inside the formula and keep the quotes around the result, which is the safest portable form. - Copy the finished column. Select the built statements, press Ctrl+C (Cmd+C on Mac), then paste into Notepad, TextEdit or another plain-text editor with Paste Special as unformatted text. This strips Excel's formatting so you get clean lines. Or paste special into the SQL client as plain text if it offers that option.
- Run a small test first. Paste just the first two or three statements into your SQL tool and run them inside a transaction if the tool supports it. Check the rows with a quick SELECT, then commit or roll back.
- Wrap the whole batch in a transaction when you are ready: put
BEGIN;at the top andCOMMIT;at the bottom, or use the tool's own transaction button. If anything fails partway through, you can roll back instead of leaving half the data inserted.
Common Mistakes
Smart quotes. Typing or pasting a formula from a word processor can turn straight quotes into curly ones. Excel will not recognise a curly quote as a string delimiter, so the formula shows #NAME? or produces odd text. Retype the quotes directly in Excel.
Forgetting to escape apostrophes. This is the single most common cause of failed batches. Any text field with a name, address or note can contain one. Always wrap text columns in SUBSTITUTE(cell,"'","''").
Quoting numbers and dates like text. The database may reject a quoted number in a numeric column, or silently store it as a string. Match the quoting to the column type.
Leaving out mandatory columns. An insert fails if a column is NOT NULL and has no default. Read the table definition first.
Using locale number formats. Values like 1,234.56 or 1.234,56 break the statement. Format numeric cells as plain numbers with no thousands separator before building the statements.
Pasting straight from Excel with formatting. The cell borders, grid and tabs travel with the copy and can corrupt the script. Paste as plain text.
Tips
Keep a separate column for the formula, never overwrite your original data. Once the statements are generated, copy that column and paste it back as values in a new sheet so the formulas do not recalculate unexpectedly.
If you have thousands of rows, generate the script in chunks of a few hundred statements per batch. Many database tools have line-length or packet-size limits, and a single enormous paste can fail for reasons that are hard to diagnose.
Consider a proper import instead when the dataset is large or recurring. Excel can save a sheet as CSV and most databases have a bulk import tool or a Power Query connector that handles escaping and types correctly. For one-off small sets the formula method is faster; for repeat work, use the real import path.
Test the generated SQL against a copy of the table, or use a development database, before touching production. Deleting and re-inserting rows in production is easy to get wrong.
Troubleshooting
The formula shows #NAME?. Check that every quote is straight, that the formula starts with =, and that you have not used a comma where a semicolon belongs in your locale. In some European spreadsheet locales the argument separator is a semicolon, which can confuse formulas copied from English tutorials.
The database reports a syntax error near a value. Look at the exact statement in the error message. Usually a stray apostrophe in the data or a missing comma. Find the row, fix the formula for that row, regenerate.
Numbers appear as text after import. You quoted them. Remove the quotes around numeric cell references and re-run.
Dates are wrong or rejected. Switch to TEXT with a yyyy-mm-dd pattern and check the column's date type. If the database expects a timestamp, add the time in the same format.
Some rows are missing after the batch. A failed statement in the middle of a transaction may have aborted the rest. Run the batch with a transaction and check the error output; most tools report the first failure and stop.
The paste into the SQL tool hangs or truncates. Split the script into smaller batches and increase the tool's query size limit if it has one. Some clients also choke on lines over a certain length, so avoid building single row statements with very long text fields.
Permission errors. The database account you are using may not have INSERT rights on that table. Ask the database owner rather than trying to work around it.
If none of this works, compare a statement that failed against one the database tool generated itself (many tools let you export a row as an INSERT statement). The difference usually shows the exact quoting or type problem.
Frequently Asked Questions
Can Excel run an INSERT statement directly?
Not natively. Excel can build the statement as text, and you can connect to some databases with Power Query or a linked table, but plain Excel cannot execute SQL against a remote database on its own. Paste the generated text into your database tool to run it.
Why does my generated SQL fail on names with apostrophes?
An apostrophe ends the string literal early. Double it in the output with SUBSTITUTE(A2,"'","''"), which is the standard way to include a single quote in a SQL string.
How do I insert several rows in one statement?
Some databases accept a multi-row form: INSERT INTO t (a, b) VALUES ('x', 1), ('y', 2);. You can build that in Excel by joining groups of rows with commas, but it is slower to get right than one statement per row, and not every database supports it. One statement per row is the safer default.
Can I include blank cells without causing errors?
Yes, but choose deliberately whether a blank becomes an empty string or NULL. Use an IF formula to output NULL for empty cells where the column allows it; otherwise an empty string is inserted as text.
Is there a faster way than copying and pasting?
For larger or recurring jobs, save the sheet as CSV and use the database's import tool, or use Power Query to load the data directly. These avoid manual quoting problems and handle data types automatically.
Why do my decimal numbers come out with commas?
The cell or column uses a regional number format. Set the column to plain number format with no thousands separator before building the formula, and leave numeric column values unquoted.