我有数百个要导入数据库表的 .xlsx 文件。我在 csv 文件中有文件的名称。我想循环遍历该 csv 文件,以使这数百个文件的导入过程更加简单。
这是我当前插入的脚本,我必须为每个文件写出。
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$]')
我的问题是我必须为每个不同的文件名重复/更改此语句数百甚至数千次。
所以接下来的几个语句看起来像这样。
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$]')