Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Insert Excel Data into SQL Tables

· 8 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Yes, you can insert data from an Excel spreadsheet into a SQL Server table, but you must first prepare the Excel file and the SQL table structure to match. The most reliable method is using the SQL Server Import and Export Wizard, which handles data types and conversions automatically, rather than writing raw T-SQL scripts that often fail with formatting errors.

What You'll Need

  • A computer running Windows with Microsoft Excel installed.
  • Access to a SQL Server database, either locally on your machine or hosted on a network.
  • SQL Server Management Studio (SSMS) installed on your machine, or the query editor in Azure Data Studio.
  • The destination SQL table created beforehand (or the option to create a new one during the import).
  • Administrator rights on the SQL Server instance to create tables or insert data.

Step-by-Step

  1. Prepare Your Excel Data
    The first step is ensuring your Excel file is clean and formatted correctly. SQL import tools are sensitive to formatting. Open your Excel file and verify that the first row contains the column headers exactly as you want them to appear in the SQL table. Ensure there are no empty columns at the start or end of your data range that you do not want to import. If your data contains numbers stored as text (for example, an ID column starting with zeros), you must leave them as text in Excel; otherwise, SQL will likely drop the leading zeros during import.
  2. Open SQL Server Management Studio
    Launch SQL Server Management Studio (SSMS) and connect to your SQL Server instance. If you are using Azure Data Studio, open it and connect to your database. It is often helpful to have the SSMS window open while you work with Excel, so you can easily reference the table structure you are about to create or target.
  3. Launch the Import and Export Wizard
    In SSMS, right-click on the database name in the Object Explorer pane, hover over Tasks, and select Import Data.... This launches the SQL Server Import and Export Wizard. If you do not see the database listed, ensure you are connected to the correct server instance and database.
  4. Select a Data Source
    In the first window of the wizard, select Microsoft Excel as the data source. Click the Browse... button and navigate to your Excel file. You will need to select the version of Excel you are using (e.g., Excel 97-2003, Excel 5.0/7.0, or Excel 12.0) from the dropdown menu. Once the file is selected, check the box labeled First row has column names. This tells the wizard to use your headers as column names in the SQL table.
  5. Specify the Destination
    In the Destination dropdown menu, select SQL Server Native Client 11.0 (or the latest version available on your system). Click the Server name dropdown and select your local server or a remote server name. If it asks for authentication, choose Windows Authentication (recommended for local development) or SQL Server Authentication if you have specific login credentials. In the Select or enter a database name dropdown, choose the database where you want to insert the data. If you have not created the table yet, you can select a database that exists and let the wizard create the table for you.
  6. Map the Tables and Columns
    The wizard will now show a list of tables and views from your Excel file on the left and your SQL database on the right. You can map specific Excel columns to specific SQL columns by clicking on the arrow between the two panes. If you want to import all columns, you can simply drag the Excel table to the SQL database. Ensure that the data types are compatible; for example, an Excel text field will map to a SQL VARCHAR, while a numeric Excel column will map to SQL INT or FLOAT.
  7. Configure Column Mappings and Transformations
    Click the Edit Mappings... button to review the data types more closely. This window allows you to change the destination data type if the default mapping is incorrect. For instance, if a numeric column in Excel is being mapped to a SQL VARCHAR, you should change it to INT or FLOAT to avoid errors. You can also specify whether to allow NULL values or reject them if they are not present. This step is crucial for preventing data truncation or conversion errors later.
  8. Run the Package
    Review the summary screen to ensure everything looks correct. You can check the boxes to Run immediately and Save SSIS Package. If you choose to save the package, it is good practice to save it to the File System or SQL Server (SSISDB) so you can run it again later without repeating these steps. Click Finish to begin the import process. The wizard will display progress bars and a summary of rows copied.
  9. Verify the Data in SQL
    Once the import completes, go back to SSMS. Expand the Tables node under your database. You should see the new table (or the existing table you imported into) populated with data. You can run a simple SELECT * FROM [TableName] query to view the data and ensure the data looks correct, particularly for dates, numbers, and text fields.

Common Mistakes

  • Leading Zeros in IDs One of the most common errors is importing IDs that have leading zeros (e.g., 00123). Excel often converts these to numbers (123), and SQL will drop the leading zeros. The fix is to ensure the column is formatted as Text in Excel before importing, or use the Fixed Width import option in the wizard.
  • Text Delimiters If your Excel data contains commas, the wizard may misread the rows. For example, a line like Smith, John will be split into two columns. To fix this, ensure your Excel data is enclosed in double quotes, or use the Advanced settings in the text file export wizard to specify the delimiter.
  • Incorrect Data Types Forcing a SQL column to accept a data type that is too small for the Excel data will result in truncation. For example, importing a date like 12/31/2024 into a SQL column defined as TINYINT will cause an error. Always check the column mappings carefully.
  • Ignoring NULLs If your SQL table does not allow NULLs and your Excel file has empty cells, the import will fail. You must either fill in the Excel cells with a default value (like 0 or an empty string) or change the SQL table column definition to allow NULLs.

Tips

  • Use Fixed Width for Complex Data If your Excel data contains varying delimiters or special characters, switching to the Fixed Width option in the Import Wizard can be more robust than the Delimited option.
  • Save and Reuse the Package Saving the SSIS package after the first successful run saves significant time. You can schedule this package to run automatically using SQL Server Agent to refresh your SQL tables with the latest Excel data on a weekly or daily basis.
  • Check for Reserved Keywords If your Excel column names are SQL reserved words (like Order or Group), the import will likely fail. Rename the columns in Excel to something else (e.g., Order_ID) before importing.
  • Disable Triggers If you are importing a large amount of data and the process is slow, temporarily disable any INSERT triggers on the destination table. This improves performance during the bulk load. Remember to re-enable the triggers after the import is complete.

Troubleshooting

If the import fails or produces incorrect data, follow these checks in order:

  1. Check for Data Type Mismatches The most common cause of failure is a data type mismatch. Open the Edit Mappings screen in the wizard and verify that text fields are mapped to VARCHAR and dates are mapped to DATETIME. If you see a red exclamation mark, click on it to see the specific error message.
  2. Verify Excel Formatting Sometimes Excel's automatic formatting can change numbers to dates or dates to text. Right-click on a problematic column in Excel and select Format Cells. Ensure that the format matches what you expect (e.g., Number, Text, or Date).
  3. Check Connection Permissions If you receive an authentication error, ensure that the SQL Server login you are using has the necessary permissions to create tables or insert data into the database. A standard login usually requires db_datareader and db_datawriter roles.
  4. Review the Error Log If the wizard fails midway through, check the Error Report at the end of the process. This report often contains a detailed log of which rows caused the failure, which is very helpful for debugging specific data issues.
  5. Try a Different Data Source If the Excel file is corrupted or severely formatted, try exporting the data from Excel to a CSV (Comma Separated Values) file and then use the CSV file as the data source in the Import Wizard. CSVs are often more robust and less prone to formatting errors.

Frequently Asked Questions

Can I import Excel data directly into an existing SQL table?

Yes, you can import into an existing table as long as the column names match, and the data types are compatible. If the existing table has extra columns that are not in the Excel file, they will remain empty. If the Excel file has extra columns that are not in the table, the import will usually fail or ignore those columns depending on the settings.

Is it better to use a T-SQL script or the Import Wizard?

The Import Wizard is generally better for beginners or for large datasets because it handles data type conversions automatically. However, for very small, specific datasets, a T-SQL script using OPENROWSET or INSERT INTO ... SELECT might be faster and more controllable once you are comfortable with SQL syntax.

What if my Excel file is password protected?

The Import Wizard does not support importing from password-protected Excel files. You must remove the password protection from the Excel file before attempting to import the data.

How do I handle dates that are formatted differently in Excel?

The Import Wizard is generally good at recognizing Excel's standard date formats. However, if you have dates in a non-standard format (like text strings that look like dates), you may need to use the Advanced settings in the wizard to specify the date format, or use the Transformations tab to change the data type before it reaches the destination.

Can I import Excel data into a SQL database on a remote server?

Yes, as long as you have network connectivity and the correct permissions. In the Destination screen of the wizard, simply select the remote server name or IP address from the dropdown menu and provide the appropriate authentication details.

What happens if I have duplicate primary keys?

If you are importing into a table with a primary key constraint and your Excel data contains duplicate primary key values, the import will fail with a primary key violation error. You must either remove the duplicates from your Excel file or disable the primary key constraint temporarily (though this is not recommended for production data).