I have hundreds of .xlsx files that I would like to import into a database table. I have the names of the files in a csv file. I would like to loop through that csv file to make the import process of these hundreds of files more simple.
Here is my current insert into script which I have to write out for each file.
INSERT INTO TableName Select *, 'FileName' as 'SurveyName'
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=C:\FileName.xlsx;HDR=YES', 'SELECT * FROM [Sheet1$]')
My issue is that I have to repeat/change this statement hundreds and eventually thousands of times for each different filename.
so the next few statements would look like this.
INSERT INTO TableName Select *, 'FileName2' as 'SurveyName'
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=C:\FileName2.xlsx;HDR=YES', 'SELECT * FROM [Sheet1$]')
INSERT INTO TableName Select *, 'FileName3' as 'SurveyName'
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=C:\FileName3.xlsx;HDR=YES', 'SELECT * FROM [Sheet1$]')
INSERT INTO TableName Select *, 'FileName4' as 'SurveyName'
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=C:\FileName4.xlsx;HDR=YES', 'SELECT * FROM [Sheet1$]')
INSERT INTO TableName Select *, 'FileName5' as 'SurveyName'
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=C:\FileName5.xlsx;HDR=YES', 'SELECT * FROM [Sheet1$]')
...
INSERT INTO TableName Select *, 'FileName700' as 'SurveyName'
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=C:\FileName700.xlsx;HDR=YES', 'SELECT * FROM [Sheet1$]')