Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import Excel to SQL Server 2008

I need to create a process to import a multi tabbed excel spreadsheet into SQL Server 2008R2. Each tab will be a different table in the database. This will need to be done weekly and imports should be automated. Ideally I want to pop the spreadsheet into a folder [or have some intern do it] and have sql run a procedure that looks in this folder, and adds the data to the tables in this db. I would also like to have another table that tracks the imports and date stamps them. I really have no idea where to even start here as I'm a pretty huge noob when it comes to tsql.

like image 905
d90 Avatar asked Oct 10 '13 17:10

d90


People also ask

How do I export data from Excel spreadsheet to SQL Server 2008?

From your SQL Server Management Studio, you open Object Explorer, go to your database where you want to load the data into, right click, then pick Tasks > Import Data. This opens the Import Data Wizard, which typically works pretty well for importing from Excel.


2 Answers

There is a nice article by microsoft - http://support.microsoft.com/kb/321686 - that outlines the processes involved.

The process is simply

SELECT * INTO XLImport3 FROM OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0',
   'Data Source=C:\test\xltest.xls;Extended Properties=Excel 8.0')...[Customers$]

Where XLImport3 is the table you want to import into and the datasource is the excel sheet you want to import from.

like image 153
Kami Avatar answered Oct 26 '22 02:10

Kami


If you're limited solely to TSQL, the above two answers will show you some ideas. If you have access to either Data Tools or Business Intelligence, with SSIS, you can automate it with the assumption that each sheet in the Excel workbook matches each time. With SSIS, you'll use a Data Flow task and each sheet will be imported into the table that you want. When you're ready for the file the next week, you'll drop it into the folder and run the SSIS package.

However, if the sheet names change, (for instance, one week sheets are called Cats, Dogs, Rain and the next week it's Sulfur, Fire, Hell) then this would cause the package to break. Otherwise, if only the data within the worksheet change, then this can be completely automated with SSIS.

Example article: https://www.simple-talk.com/sql/ssis/moving-data-from-excel-to-sql-server---10-steps-to-follow/

like image 40
Question3CPO Avatar answered Oct 26 '22 00:10

Question3CPO