简体   繁体   中英

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'

从这张图

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?

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.

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: 在此处输入图片说明

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

在此处输入图片说明

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

在此处输入图片说明

And... That's it! Easy peasy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM