question

rsirinek avatar image
rsirinek asked

Load Excel/Delimited files as tables in SQL server

I am being asked to load data for the data quality assurance team. Someone else is extracting the data from the application data source for me. I receive the data as .xls/.xlsx files or sometimes as a delimited file if there are more than 300k records. So when anywhere from 20 to 60 files are handed over to me, I am expected to load the data in a 'few hours' to a half day. **What would be your first approach the task described above? There are no answers out of scope.** I have all the tools you'd probably mention. I am only limited by my experience.... what do you think SSIS? The data quality team likes the data loaded with the appropriate data type (not all just VARCHAR(255)). Just a few requirements: Each file needs loaded as it's own table(that does not exist) and to use the file's header row as the fields and name the table using the filename. Here are the two approaches I've been using: 1.) I have been using an VB module which converts all xls files in a directory to Access objects then uses an ODBC connection to upload the each file as a table into SQL. The drawback is that it runs slowly from my local machine. Will it run faster if the file is run on the server and possibly change the connection from ODBC to something else. Code below: 2.) Another method I used was BULK INSERT where I a.) use the header row to create the table and then b.) load the file with BULK INSERT **What is a better approach? Is there a SSIS tutorial on how to load a directory of files each as its own table into SQL Server?** I created a package from this tutorial: http://beyondrelational.com/modules/12/tutorials/24/tutorials/9011/getting-started-with-ssis-part-6-import-export-wizard-part-1.aspx Could a loop be added to repeat the process for multiple files?
ssissql-servertsqldatatypesvb
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Fatherjack avatar image Fatherjack ♦♦ commented ·
Can you request that all files are the same format? Having them all delimited will be a big help. How can you tell the datatype from a csv file? surely they need to provide the DDL with the data?
0 Likes 0 ·
Kash avatar image
Kash answered
This is what I would do: 1. Get the files in csv (comma seperated format if possible) 2. Use a loop to go through all the files. 3. Use Bulk Insert to load data, here is the syntax on Bulk Insert: 4. BULK INSERT DBO.LIST FROM '\\\Server\Data Projects\\LIST.txt' WITH (FIELDTERMINATOR = ',', FIRSTROW=2 ) p.s. One option is to use xp_cmdshell when working with OS files - Kash Mughal - Senior DBA and Trainer - http://www.sqlserver2012tutorial.com
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Phil Factor avatar image
Phil Factor answered
I would link to them either as an Excel or Text(CSV) source. Sure, it isn't as fast as BCP, but you are keeping all that import stuff at arms length and don't then need staging tables. You can perform SQL Queries on the linked tables in order to import the data, and do all your transformations in SQL. Robyn and I explained quite a bit about how to do it here [ http://www.simple-talk.com/sql/t-sql-programming/sql-server-excel-workbench/][1] and I recently showed how to do CSV format imports via a linked server here [ http://www.simple-talk.com/sql/t-sql-programming/the-tsql-of-csv-comma-delimited-of-errors/][2] [1]: http://www.simple-talk.com/sql/t-sql-programming/sql-server-excel-workbench/ [2]: http://www.simple-talk.com/sql/t-sql-programming/the-tsql-of-csv-comma-delimited-of-errors/
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Jeff Moden avatar image Jeff Moden commented ·
I'd love to see an updated article that uses the ACE drivers, Phil.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.