简体   繁体   English

是否有一种简单的方法,可以使用SSIS将多个Excel文件中的数据导入SQL,包括文件名?

[英]Is there a simple method to import data from multiple excel flies to SQL including names of the files using SSIS?

I need to import salary data from multiple excel files where filename of each file is a date. 我需要从多个Excel文件(每个文件的文件名是一个日期)中导入薪水数据。

I used SSIS and with success followed typical tutorials for importing multiple excel files. 我使用了SSIS,并成功地遵循了导入多个Excel文件的典型教程。 The thing is none of them show simple method how to add one extra column (with the name of the file) to the result. 事实是,它们都没有显示出简单的方法如何在结果中添加一个额外的列(带有文件名)。 There are some tutorials with huge code scripts, that are too complicated for me. 有些教程包含巨大的代码脚本,对我来说太复杂了。

What I did was to add 'Derived column' module between typical 'excel source' and 'OLE BD destination' where I added a new column 'date' with expression @[User::FileName] - a variable that is used for 'foreach loop container' but as a result I received corectly combined data from all files but the extra column contains the same data - the filename of the first imported file. 我所做的是在典型的“ excel源”和“ OLE BD目标”之间添加“派生列”模块,在其中我添加了带有表达式@ [User :: FileName]的新列“ date”-用于“ foreach”的变量循环容器”,但结果是我从所有文件中接收到完全合并的数据,但额外的列包含相同的数据-第一个导入文件的文件名。

I wonder if there is any simple to make the variable I used to change with every loop ? 我想知道是否有任何简单的方法可以使我每次循环都要更改的变量? So, as a result, I receive combined data plus one extra column containing the corresponding date, which is the name of each file. 因此,结果是,我接收到组合数据以及包含相应日期的额外一列,该日期是每个文件的名称。 Many Thanks 非常感谢

If you are using a Foreach File Enumerator , select "Name only" in the Collection pane as in the image below (I'm assuming that "Name only" will give you the date that you are looking for). 如果您使用的是Foreach File Enumerator ,请在“ Collection窗格中选择“仅名称”,如下图所示(我假设“仅名称”将为您提供所需的日期)。

收藏窗格

This allows you to map this into a variable on each iteration. 这样,您可以在每次迭代时将其映射到变量中。 To do this, navigate to the Variable Mappings pane, and select the variable you want to use in your Data Flow Task , with 0 as the Index. 为此,请导航至“ Variable Mappings窗格,然后选择要在“ Data Flow Task使用的变量,索引为0。

可变映射窗格

You can then add this variable as a Derived Column , and it will give you the name of the file you are importing. 然后,您可以将此变量添加为Derived Column ,它将为您提供要导入的文件的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM