Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSIS Foreach Loop Folder as variable

Tags:

I need to set the Folder in my Foreach loop to a variable location. I tried just adding the location to my package configuration, and it acted like it worked, but when I opened the configuration file, it wasn't there.

I had it set to Foreach File Enumerator, with the path hardcoded, and Retrieve fully qualified file name. My variable Mappings have a variable CurrentFile with index 0.

So, I've changed it to a Foreach From Variable Enumerator, with a SourceFiles variable of \server\path*.*. But now it's complaining about that CurrentFile, that the type of value being assigned differs from the variable type (string).

I'm guessing that I need to somehow use the Expressions with the Enumerator on the Collections tab? How?

  • General: Foreach Loop Container
  • Collection: Foreach From Varialbe Enumerator; Variable is User::SourcePath; Expressions (not used)
  • Variable Mappings: Variable User::CurrentFile, Index 0
  • Expressions: (not used)

Error: 0xC001F009 at MyPackage: The type of the value being assigned to variable "User::CurrentFile" differs from the current variable type. Variables may not change type during execution. Variable types are strict, except for variables of type Object. Error: 0xC001C012 at Foreach Loop Container: ForEach Variable Mapping number 1 to variable "User::CurrentFile" cannot be applied.

like image 635
thursdaysgeek Avatar asked Mar 29 '11 00:03

thursdaysgeek


People also ask

How do I create a foreach loop container in SSIS?

In the SSIS Toolbox, expand Containers, and then drag a Foreach Loop Container onto the design surface of the Control Flow tab. Right-click the new Foreach Loop Container and select Edit. In the Foreach Loop Editor dialog, on the General page, for Name, enter Foreach File in Folder. Select OK.

How does foreach loop work in SSIS?

The Foreach loop uses the Foreach File enumerator, and the File System task is configured to copy a file. If the folder that the enumerator specifies contains four files, the loop repeats four times and copies four files.

What is the difference between for loop and foreach loop container in SSIS?

A foreach loop is very similar to a for loop, except there is no evaluation phase. This is because you are looping over a collection of objects, and for each object in the collection, you execute a specific statement.


1 Answers

A co-worker showed me how to get it to work. Do NOT change it to "Foreach from Variable Enumerator", but leave it as "Foreach File Enumerator".

  • Collection: Foreach File Enumerator; put in a folder and files in the Enumerator configuration. Under Expressions, set the Directory to a user variable pointing to the directory (it will take precedence to the folder you've entered); set the FileSpec to a user variable pointing to the files (which will take precendence to the Files you've entered).
Directory @[User::SourcePath]  (value: \\server\directory)
FileSpec @[User::CopyFileName] (value: *.*)
  • Variable Mappings: Variable User::Current File, index 0
  • Expressions: (not used)
like image 107
thursdaysgeek Avatar answered Oct 07 '22 20:10

thursdaysgeek