Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 SSIS and Import/Export (2008)
 Multiple Excel imports into a single database

Author  Topic 

gdawdy
Starting Member

1 Post

Posted - 2012-12-17 : 14:45:50
I'm still fairly new to writing SQL scripts. I have a script which imports numerous excel files (possibly reaching 1000+). Some of these Excel spreadsheets have only one row and some have more than 50 rows. I'm importing all of these excel spreadsheets into one table and would like to be able to add a column which identifies which Excel spreadsheet, by filename, the record came from. So rows 1 through 10 may come from ExcelSpreadsheetA.xlsx and rows 11 through 15 may have come from ExcelSpreadsheetB.xlsx. Would I be able to set this identification up during the import process?

I use this script to perform my import from excel:

select *
into SQLServerTable
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=D:\testing.xls;HDR=YES',
'SELECT * FROM [Sheet1$]')

I then have additional statements for each additional Excel spreadsheet which then append to the table.

What I would like to do is add logic to the end of that script that says something like:

update sqlservertable
set filename = testing.xls


But I want it to set the filename attribute for just the records that had been imported from that specific filename. Ideally I would like the final table to look similar to the one below.

Col1 Col2 Col3 Filename
===== ===== ===== =========
1 A B export1.xlsx
2 C D export1.xlsx
3 E F export1.xlsx
4 G H export5.xlsx
5 I J export8.xlsx
6 K L export8.xlsx

Hommer
Aged Yak Warrior

808 Posts

Posted - 2013-01-02 : 14:47:09
will this work?

select *, 'testing.xls'
into SQLServerTable
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=D:\testing.xls;HDR=YES',
'SELECT * FROM [Sheet1$]')

Go to Top of Page
   

- Advertisement -