Tsar
Tsar

Reputation: 170

Importing data from Excel to SQL Server

I just tried to add data from Excel file to SQL Server. I can import as a whole new table, but that means I have to assign the indexes again. Is it possible to import into an existing table?

I saw another question similar to this, and I tried what was suggested there, clicking 'write a query to specify the data transfer'

From this diagram

I entered the following query

INSERT INTO Customer (Customer_Id, Customer_Name, Customer_Company, Address, Phone)
   SELECT 
      A.[Column1], A.[Column2], A.[Column3], A.[Column4], A.[Column5]
  FROM 
      OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=D:\Excel.xls;HDR=YES', 'select * from [Sheet1$]') AS A;

I don't really understand the syntax of the second part of it.. and I'm getting a syntax error in the FROM clause.

Any clues?

Upvotes: 0

Views: 3097

Answers (2)

Fábio Carvalho
Fábio Carvalho

Reputation: 103

You can just copy and paste excel from cells in sql server. :DD

I'm seeing this right now, when I'm facing same problem as you.

This is what I did:

I copy my data from excel: enter image description here

Next in the sql server, I order my table to generate script for edit 200 rows:

enter image description here

And then in the last line on edit rows, you shoul past the values:

enter image description here

And... That's it! Easy peasy

Upvotes: 2

Greg the Incredulous
Greg the Incredulous

Reputation: 1846

The simplest way to import data from Excel for a one off import is to use the SQL Server Import and Export Wizard as shown in the picture in the question. You should choose Microsoft Excel as the data source, specify the file path to your spreadsheet, and specify whether the first row contains column headers. Then specify the target SQL server and database.

Choose the other option from the one selected in your diagram - "Copy data from one or more tables or views". You can then select the worksheet within your spreadsheet that you want to import, and specify the target table you want to put the data into. Importantly you can edit the mappings so that the data from your spreadsheet goes into the correct columns in your table - either an existing table or a new table. Behind the scenes the wizard is generating a similar script to the one in your question but you should not have to worry about it if you set up the wizard correctly.

From memory you can also convert this into a SSIS package so it is a repeatable process.

Upvotes: 2

Related Questions