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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 SSIS and Import/Export (2008)
 Multiple Excel imports into a single database
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

1 Posts

Posted - 12/17/2012 :  14:45:50  Show Profile  Reply with Quote
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

Aged Yak Warrior

808 Posts

Posted - 01/02/2013 :  14:47:09  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000