简体   繁体   中英

Importing from Excel or a CSV file

I need to import data to a sql table finding a row by column A and putting column B and C into the row into specific data columns.

My excel/CSV file is similar to:

Code,Unit,Price
1234,Pack10,1.20
4567,Each,3.40

My table is similar to

ItemID Code Name ItemPrice Unit Supplier LastCost SellBy QtyStock Inactive

I need to update the table from the lines in the spreadsheet finding the code row and replacing the Unit and the ItemPrice columns.

I have tried using the standard import data but cannot see how to find the code and then insert the required fields.

Any help or point me to where I can find an answer will be most appreciated.

Breakup the problem into two smaller steps... First, do just the standard import data, and place the contents of your excel file into a SQL table. After you've imported the data, perform just a standard SQL update statement to join together your imported table and your existing table to update the existing table.

UPDATE e
SET
    Unit = i.Unit,
    ItemPrice = i.Price
FROM
    ExistingTable e JOIN
    ImportedTable i ON i.Code = e.Code

To make it in a single step you can use OPENROWSET as follows:

UPDATE A
   SET [ItemPrice] = B.Price,
       [Unit] = b.Unit
FROM   [SO].[dbo].[PricesTbl] A INNER JOIN
       OPENROWSET('Microsoft.Jet.OLEDB.4.0',
       'Excel 8.0;Database=C:\Prices.xls', 'SELECT * FROM [Sheet1$]') B ON A.CODE=B.CODE

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