SSIS ForEach File loop - insert file name to table

ぃ、小莉子 提交于 2021-02-08 03:41:09

问题


I'm building a SSIS package (using VS 2017) to load a bunch of CSV files from a specific folder. It works great using the ForEach File loop. Data Flow task has a Flat File Source and OLE DB Destination. I want to be able to save the filename in the same table, along the data from the CSV file. How can I do this??

thanks a lot for your time and help


回答1:


One other method if you want to load the entire file path of the flat files is

  • Right click on Flat File Source in Data Flow tab
  • Click "Show Advanced Editor"
  • Click "Component Properties"
  • Under Custom Properties you will find "FileNameColumnName".
  • If you give a name (e.g.: FlatFileName) then that will appear as one of the source output columns in mapping that contains the file name value(full path with file name). It can be mapped to any available column in destination or modified using Derived column task to get only file name.

In my development, I have stored mostly the entire path which helped me in better tracking.

One other method for anyone who comes across this question, instead of using ForEach loop you can also use a simpler method

  • Right click in Connection Manager
  • Click on "New Connection"
  • Select "MULTIFLATFILE" connection type and click Add.
  • In Connection manager editor, insert location and use wildcard * (E.g.: \\ABC\XYZ\file_*.txt) to pick all the flat files in that folder.
  • This automatically loops through all the flat files in that folder.

Hope this helps!




回答2:


The ForEach File Enumerator can capture the file name and assign it to a variable, which can then be routed to the connection string variable of a connection manager for dynamic loading. This same methodology can be applied to write the file name to a database table.

In the data flow, add a Derived Column transformation and add a new column called 'FileName' (or whatever) and then set it's value to the variable value that the ForEach File Enumerator is setting for the file name.



来源:https://stackoverflow.com/questions/54525384/ssis-foreach-file-loop-insert-file-name-to-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!