excel to sql help
What's the best way to get my excel file into my 2 target tables? The action column tells whether I want the record inserted or deleted from tables. The tables are Fruits and FruitTypes. Is it possible to query into Excel directly instead of importing to temp table (not that that's bad or not allowed, just curious, not important)? I'd like to be able to run it anytime without risk of overwriting.
Excel
------
FruitCode,Desc1,FruitType,Desc2, Action
--开发者_运维知识库---------------------------------
Ap,Apple, Apple1, Apple Type 1, INS
Ap,Apple, Apple2, Apple Type 2, INS
Ap,Apple, Apple3, Apple Type 3, DEL
Or,Orange, Orange1, Orange Type 1, INS
Or,Orange, Orange2, Orange Type 2, INS
SQL Tables
-----------------
Table: Fruits (PK: FruitCode)
----------
FruitCode, Desc1
-----------------------
Ap, Apple
Or, Orange
Table: FruitTypes (PK: FruitCode, FruitType)
-----------
FruitCode, FruitType, Desc2
-----------------------
Ap,Apple1, Apple Type 1
Ap,Apple2, Apple Type 2
Or,Orange1, Orange Type 1
Or,Orange2, Orange Type 2
You could set up the Excel file as a datasource per this article:
http://support.microsoft.com/kb/306397
Or.. you could use SQL Server Management Studio Import Data wizard.
Right click database, Tasks -> Import Data
Here's a good walkthroug on Importing and Exporting: http://msdn.microsoft.com/en-us/library/ms140052.aspx
and a specific HowTo for Excel -> SQL using this process: http://dotnetslackers.com/articles/sql/Importing-MS-Excel-data-to-SQL-Server-2008.aspx
edit: This approach does mean you would be importing the data, rather than just looking at it. If you don't want to import it, then look at some of the other answers around connecting to Excel using ODBC.
The easiest way is to use dts if you have sql server (2005 or 2008). If you have a version other than the free version you can right-click on the db in the sql server design and select Tasks, then Import data and follow the prompts.
精彩评论