Insert Excel Values into SQL Table
In this article
To insert values from an Excel file into a SQL table, you convert the spreadsheet data into a format your database can accept—typically CSV or a direct import via a tool—and then use SQL commands or a database client to load the data. This process automates data entry, reduces manual errors, and ensures consistency across systems. You need access to the SQL database, the Excel file with clean data, and a tool like Microsoft SQL Server Management Studio (SSMS), MySQL Workbench, or pgAdmin depending on your database type.
What You'll Need
- An Excel file with structured data (columns match SQL table fields)
- Access to your SQL database (username, password, server address)
- A SQL client tool: SQL Server Management Studio (SSMS) for SQL Server, MySQL Workbench for MySQL, or pgAdmin for PostgreSQL
- Administrative permissions to insert data into the target table
- Matching data types between Excel columns and SQL table columns (e.g., text in Excel → VARCHAR in SQL)
Step-by-Step
- Open your Excel file and ensure each column header matches the exact name of a column in your target SQL table. Remove any blank rows or columns, merged cells, or special characters in headers.
- Save the Excel file as a CSV (Comma Separated Values) file: go to File > Save As, choose CSV (Comma delimited) (*.csv) from the file type dropdown, and save it to an easy-to-find location like your desktop.
- Open your SQL client tool and connect to your database using valid credentials. Navigate to the database and table where you want to insert the data.
- Verify the SQL table structure: run a query like
DESCRIBE tablename;(MySQL) orEXEC sp_columns tablename;(SQL Server) to confirm column names, data types, and whether any columns are NOT NULL without defaults. - In SQL Server Management Studio, right-click the target table, select Tasks > Import Data. In the wizard, choose Flat File Source as the data source, then browse to your CSV file. Set the delimiter to comma and check Text qualifier as double quotes if your data contains commas.
- On the Destination tab, select your database and the target table. Click Map to confirm Excel columns align with SQL columns. The wizard will auto-map by name if headers match exactly.
- Click Next, then Finish to run the import. Review the summary for errors. If any rows fail, the tool will log why (e.g., data type mismatch, NULL in required field).
- For MySQL or PostgreSQL, you can use the
LOAD DATA INFILEorCOPYcommand instead. For example, in MySQL:LOAD DATA INFILE '/path/to/your/file.csv' INTO TABLE tablename FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 ROWS;Make sure the file is accessible to the MySQL server and the server has file permissions. - After the import completes, run a
SELECT COUNT(*) FROM tablename;query to confirm the number of inserted rows matches your Excel data.
Common Mistakes
- Not saving as CSV first—Excel’s native .xlsx format is not readable by most SQL import tools.
- Using column headers with spaces, symbols, or mixed case (e.g., "First Name" instead of "first_name"). SQL expects exact matches or proper quoting.
- Ignoring data types: importing text into a DATE or INT column causes failure. Ensure dates are in YYYY-MM-DD format and numbers have no commas or currency symbols.
- Forgetting to skip the header row in SQL import commands. If you don’t use IGNORE 1 ROWS, the first row becomes a data entry and likely fails.
- Placing the CSV file on your local machine when the SQL server runs remotely. The server must have access to the file path, so move it to a shared network drive or use a tool like SSMS that can read from your local machine.
Tips
- Test with a small subset—copy 5–10 rows of your Excel data into a new CSV and import them first. This catches formatting errors before bulk insertion.
- Use a text editor like Notepad++ to open the CSV and verify there are no hidden characters or inconsistent line endings.
- Always backup your SQL table before importing. Use
CREATE TABLE backup_table AS SELECT * FROM tablename;to create a snapshot. - If your Excel has formulas, copy the values only (Ctrl+C, then Paste Values) before saving as CSV. Formulas won’t resolve in the CSV and may leave errors.
- For large datasets (10k+ rows), disable indexes on the target table during import, then rebuild them afterward to speed up the process.
Troubleshooting
If the import fails, start with these checks: First, confirm the CSV file opens correctly in a text editor and has one row per record with consistent commas. Second, verify the SQL table allows INSERT permissions—try running a simple INSERT INTO tablename (col1, col2) VALUES ('test', 1); to confirm access. Third, check for trailing spaces in column names in both Excel and SQL. Rename them if needed. Fourth, if using MySQL, ensure the local_infile system variable is enabled by running SHOW VARIABLES LIKE 'local_infile'; If it’s OFF, you’ll need to enable it via the server config or use a client tool that handles uploads. Fifth, if SSMS says "File not found," the file path may be incorrect or the server doesn’t have access; copy the CSV to the server machine or use a UNC path like \\server\share\file.csv.
Frequently Asked Questions
Can I import Excel directly without saving as CSV?
No, most SQL tools require a flat file like CSV. Excel .xlsx files are binary and incompatible. Always convert to CSV first.
What if my Excel has dates in MM/DD/YYYY format?
Convert them to YYYY-MM-DD before saving as CSV. SQL expects ISO format. Use Excel’s TEXT function: =TEXT(A1,"yyyy-mm-dd") to reformat.
Do I need to delete existing data before importing?
Not necessarily. If you want to replace data, use TRUNCATE TABLE tablename; first. If you want to append, leave it as is. Be cautious—imports can duplicate records if primary keys aren’t handled.
Can I import into a table with foreign key constraints?
Yes, but ensure referenced data exists first. If importing orders with customer IDs, the customer records must already be in the customer table. Import parent tables before child tables.
Why does my import say "Cannot convert value"?
This usually means a number in Excel has commas (e.g., 1,000) or a text field has quotes without being enclosed. Clean the data in Excel: remove formatting, use Find & Replace to eliminate commas, and wrap text fields in quotes.
Can I automate this process every week?
Yes, using SQL Server Agent (for SQL Server) or cron jobs (for MySQL/PostgreSQL) to run import scripts. Save your import as a package or script, then schedule it to run at a fixed time.