• The Excel and SQL Server experts blog
  • How to insert data from Excel to SQL Server

    Johannes Åkesson - Mar 21, 2022

    In this article, you’re going to learn 2 easy ways to perform one of the most useful data management tasks: how to insert data from Excel to SQL Server.  

    Background

    Before I founded SQL Spreads (an Excel Add-In to Import and Update SQL Server data from within Excel), I worked as a Business Intelligence consultant for many years using Microsoft’s BI-tools, such as SQL Server, SSIS, Reporting Services, Excel, etc.

    I’ve found that when working on different projects, I tend to snap up a number of great-to-know things that I can re-use over and over again. One of these things that I re-use in almost every project is the ability to copy and paste data from Excel into a table in SQL Server.

    It’s a really simple and convenient way to quickly import data into a table via SQL Server Management Studio.  For example, populating a new dimension table, adding some test data, or inputting any other data that you need to quickly get into a table in SQL Server.

    But what if you want to insert data from Excel to SQL without using Management Studio?  What if there was a way to do this directly from Excel?  This is where the SQL Spreads Excel Add-In that I’ve been working with over the last few years comes in.  It makes your Excel to SQL Server import tasks much easier to do!

    In this article, I’m therefore going to explain how to insert data from Excel to SQL Server using these 2 easy methods:

    • Insert directly from Excel to SQL tables using SQL Spreads
    • Copy and paste from Excel to SQL tables via SQL Server Management Studio

    How to import data from Excel to SQL Server – SQL Spreads method

    This method is ideal for users that don’t want to insert data using SQL Server Management Studio.

    Install the SQL Spreads Add-In for Excel

    You can download a free trial of SQL Spreads from here.

    1. Run the SQLSpreadsSetup.exe file and follow the instructions.
    2. Restart Excel and accept the Add-In confirmation.
    3. You will find SQL Spreads in the tab menu in Excel:

    For more details about installation, check out the Installing SQL Spreads section of our Knowledgebase.

    Connect to your SQL Server database

    Once SQL Spreads is installed, you’ll see that it has been added as a new ribbon tab.

    1. Click on SQL Spreads and then click the Design Mode button
      sqlspreads_click_design_mode
    2. In the SQL Spreads Designer panel on the right side, click the Edit button to open the SQL Server connection dialog.
      sqlspreads_editconnection
    3. Enter the name of your SQL Server into the SQL Server name field:
      sqlspreads_connection_details
    4. Select if you should connect using your Windows-login (Windows Authentication) or enter a user name and password (SQL Server Authentication). Windows authentication is the more secure of the two options.
      Select SQL Server authentication method
    5. Click OK. SQL Spreads will try to connect to the database. If the connection is successful, your databases will show up in the SQL Spreads Designer panel.
      SQL Spreads Designer database list
    6. Now that we’ve created the connection from Excel to SQL Server, we can select which table of data we want to use in Excel. In the SQL Spreads Designer, click on the database and then select your table.
      SQL Spreads Designer AW Table list

    As soon as you select a table, the data in the table is populated in the Excel sheet.  You can now see all the data in your SQL Server table and use it in your Excel workbook. The real power with SQL Spreads is the ability to update or add to the data in SQL Server direct from Excel.

    Inserting new rows into SQL Server

    To import new data into SQL Server, scroll down to the first empty row and either type in your new data or paste a set of rows copied from another Excel workbook:

    Insert new rows from Excel to SQL Server

    Once you’ve added or pasted the new rows, click the ‘Save to Database’ button to get the changes written to the table in SQL Server.

    Updating existing data in SQL Server

    You can also update the prices in the product table directly in Excel, and save the changes back to SQL Server.  To do this you simply make the edits in the table in Excel and then click on the ‘Save to Database’ button to get the changes written to the table in SQL Server.

    Quote example Save to Database

    How to import data from Excel to SQL Server – Copy and Paste method

    Step-by-step Instructions

    1. Open SQL Server Management Studio and connect to your SQL Server database.
    2. Expand the Databases and the Tables folders for the table where you would like to insert your data from Excel.
    3. Right-click the table and select the fourth option – Edit Top 200 Rows.
    4. The data will be loaded and you will see the first 200 rows of data in the table.
    5. Switch to Excel and select the rows and columns to insert from Excel to SQL Server.
      Right-click the selected cells and select Copy.
    6. Switch back to SQL Server Management Studio and scroll down to the last row at the bottom and locate the row with a star in the left-most column.
    7. Right click the star in the column header and select Paste.
    8. You have now completed your SQL Server import, and your data from Excel is now in a table in SQL Server!

    Remember: Always start with copying and pasting a single row of data from Excel to SQL Server. This is to check that there are no mismatches between your data from Excel and the SQL Server table (such as the number of columns) and that your data in Excel validates with the data types in the SQL Server table. See the section “Tips and tricks” below for more details.

    How to insert data from Excel to SQL Server with an identity column

    The same technique can also be used to copy and paste data into tables that have an auto-incrementing ID column (identity column).

    The thing to keep in mind here is to also include an extra left-most blank column in Excel when copying the data from Excel to SQL Server.

    Follow these steps to copy and paste the data from Excel to SQL Server using a table with an auto-incrementing ID column:

    1. Open SQL Server Management Studio and connect to your SQL Server database.
    2. Expand the Databases and the Tables folders for the table where you would like to paste the Excel data.
    3. Right-click the table name and select Edit Top 200 Rows, the fourth option from the top.
    4. This will bring up a grid with the first 200 rows of data in the table.
    5. Switch to Excel and select the rows and columns to copy. Do not include the header row.
      Now, also remember to include an extra blank left-most column in your selection.

      Then, right-click the selected cells and select Copy.
    6. Switch back to SQL Server Management Studio, and select the tab with the 200 rows from your table.
      Go to the last row at the bottom and locate the row with a star in the left-most column.
    7. Right-click on the star and select Paste.
    8. Your data from Excel is now pasted into your table in SQL Server, and SQL Server will automatically create the values in the ID/key column for you:

    Copy and paste data from Excel to SQL Server Views

    The copy and paste method also works when your Excel to SQL Server import is to a View as opposed to a Table.  The only requirement is that the View should only contain data from one table.

    In a View in SQL Server that contains data from several joined tables you cannot insert new rows, but you can update the data, as long as you only update columns that originate from the same base table.

    Excel to SQL Server import on a remote machine

    When working with SQL Server databases on a remote machine, where you connect to the remote machine using a Remote Desktop Connection, you can still use the same copy and paste technique to move the data from your local machine’s Excel to the SQL Server database on your remote machine.

    If you are not able to copy and paste the data into your SQL Server when connected using a Remote Desktop Connection, first check that copy and paste is enabled for the Remote Desktop Connection:

    1. Open the Remote Desktop Connection.
    2. Click the Show Options…
    3. Select the Local Resources tab, and then check that the Clipboard property is checked:

    If you still cannot copy and paste data between Excel on your local machine and SQL Server on your remote database server, verify with your server administrator that the copy and paste feature is enabled for the Remote Desktop Connection on the server.

    Tips when copying and pasting data from Excel to SQL server

    Validating your data – start with one row of data

    If the data that you copy from your Excel document does not match the data types of the columns in your SQL Server table, the inserting of the data will be canceled and you will get a warning message. This will happen for every row you paste from Excel to SQL Server. If you paste 500 rows from Excel with the wrong number of columns, you will get one warning message for each and every row that you paste.

    To avoid this, the trick is to start to copy only a single row of data and paste it into the SQL Server table. If you get a warning message for incorrect data types, you can correct the mismatch and repeat the copy and paste procedure until all your Excel columns fit into the table in SQL Server. When all columns match, select the remaining rows and paste them all into the SQL Server table in one step.

    Inserting NULL values from Excel into a SQL Server table

    When you have columns in your SQL Server table that allow NULL values, and you want to insert a NULL value into the table, just enter the text NULL into the cell in Excel, and then copy and paste the data from Excel into SQL Server:

    The NULL values will be inserted into the table in SQL Server:

    Tables with computed columns

    For SQL Server tables containing computed columns, you can paste data from Excel into that table simply by leaving the data for the computed column blank in Excel, and then copying and pasting the data from Excel into the SQL Server table.

    How to get the column names from the table in SQL Server to Excel

    When you prepare the data in Excel for import into an existing SQL Server table, it is useful to have the column headings and a few rows of sample data as a reference in Excel.

    There is a technique where you can copy existing data in SQL Server to Excel and include the table column names as header names.

    Follow these steps to also include the column names when copying a few rows of data from a SQL Server table into Excel:

    1. In SQL Server Management Studio, locate your database and expand the Tables folder.
    2. Right-click your table name and select the third option – Select Top 1000 rows.
    3. Select the rows to copy to Excel by holding down the CTRL button and clicking the row numbers on the left side.
    4. When your rows are selected, right-click one row and select the Copy with Headers option:
    5. Go to Excel and paste the data into a cell. The headers from the table in SQL Server will now be added as the first row:

    Excel to SQL Server performance

    Copying and pasting data from Excel to SQL Server is a really simple method to import data from Excel into your SQL Server database. One of the drawbacks is that it is not the fastest method if you need to insert larger amounts of data, such as several hundred thousand rows of data or more.

    To get a reference to the performance limits, I have run a few tests on my local i7 machine with 8 GB of RAM with Microsoft Excel and SQL Server installed on the same machine.

    I had the following results: copy data in Excel with 10 columns of mixed data types to SQL Server took about 2 seconds for 100 rows, about 30 seconds for 1000 rows, and about 10 minutes for 20,000 rows.

    So, I would say that the limit to use the copy and paste feature is around a few thousand up to a few tens of thousands of rows of data.  If you need to perform an Excel to SQL Server import with more data, then you should use the SQL Server Import and Export Wizard.

    Copy and paste – a quick reference

    • First, copy the data from Excel, and then paste it into the SQL Server table using the Database > Table > Edit top 200 rows menu option.
    • Always start by copying and pasting a single row of data to validate the data types.
    • For SQL Server tables with an identity column, add an extra blank left-most column before copying from Excel.
    • Copy and paste from Excel to SQL Server can be used to insert up to a few tens of thousands of rows of data.
    • To get the SQL Server column headers into Excel, right-click the table in SQL Server and select Copy with headers.
    • Don’t forget that the technique also works great over Remote Desktop Connections!

    Other tools and techniques

    There are some other ways to import an Excel file to a table in SQL Server. Here are some of the other methods.

    • SQL Server Import Wizard – a wizard-based import tool inside SQL Server Management Studio. Ideal for one-time imports when you have an Excel document that you need to import into a table in SQL Server. The pros include flexibility and lots of settings to finetune the import. The biggest drawback is that you need to run through a dozen Wizard dialogs with lots of settings each time you need to import the data. More info about the SQL Server Import Wizard is available here.
    • SSIS – this is the oil tanker for moving data between different sources. You can do almost any task you like, but you will need to put in lots of time to get started, and it will take still more time to maintain and change the solution down the line. The pros include good versatility and plenty of available features; the main con is the time you will have to put in to learn the tool. More info about SSIS is available here.
    • The BCP utility – a command line-based tool that offers a huge number of settings – if you are a coder, this is the tool to use. More info about the BCP utility is available here.

    Summary – insert data from Excel to SQL Server

    In this article, we’ve looked at 2 easy ways to insert data from Excel to SQL Server.

    If you know how to use SQL Server Management Studio, the copy and paste feature is a great option when you need to quickly and easily import data from Excel to SQL Server. The process is simple and doesn’t require any special knowledge or tools, and can be used in tables with up to a few tens of thousands of rows of data.  It can also cater for scenarios such as tables with an auto-incrementing identity key, or if you need to connect to SQL Server on a remote machine using a Remote Desktop Connection.

    If you don’t have access to SQL Server Management Studio, you can use the SQL Spreads Excel Add-In to insert data from Excel to SQL.  It’s quick and easy to use for non-technical users.  For more advanced users, there are some cool features such as lookup columns, pivot options, and data validation which allow you to create robust data management solutions.

    Download your free trial of SQL Spreads and get in touch with us if you have any questions.

    Leave a Reply


    Comments (7)

    Reply
    Dan » 20. Dec, 2018

    Thanks for showing the super simple copy/paste functionality. I couldn’t get the Import Wizard to work.

    Reply
    Jim C » 06. Dec, 2019

    Really helpful, especially the explanation of possible errors.

    Reply
    Jeff » 16. Dec, 2019

    Very helpful thanks Johannes!

    Reply
    Prakash » 10. Aug, 2021

    Very nice information with screenshot. Well done.

    Reply
    Allan Rodríguez » 18. Jul, 2022

    Thanks a lot for this helpful information!

    Reply
    Herbert » 28. Jul, 2022

    Very Helpful. Please upload other tips and tricks for Data stuffs. Thanks.

Use Excel to update your data in SQL Server?

Try the SQL Spreads data management solution to update and manage your SQL Server data from within Excel.