sql server 2008 - Creating an Expression for an Object Variable? -


i creating ssis package , first step list of files stored in file , store information in object variable. pass variable task tries open list of files. however, open file task fails if there no files open because folder empty.

i relatively new using ssis , curious on how rewrite expression checks see if object variable empty , if empty not allow open file task run.

you don't need use object variable loop through files in folder. can achieve using foreach loop container. following example demonstrate how can done. example created in ssis 2008 r2.

step-by-step process:

  1. let's assume need loop through files in path c:\temp shown in screenshot #1.

  2. on ssis package, create 3 variables namely folderpath, filepattern , filepath. refer screenshot #2. set folderpath variable folder loop through, here in case have chosen c:\temp. set filepattern variable pattern files should match, here loop through files have used *.*. if loop through excel 2010 files, can use *.xlsx. can accept 1 pattern. don't set value variable filepath because assigned value when foreach loop container loops through each file in folder.

  3. on package's control flow tab, place foreach loop container , place script task within foreach loop container. in example, going loop through each file , display names , not more. refer screenshot #3.

  4. configure foreach loop container shown in screenshots #4 , #5. on collection section, have configured expressions use variables folderpath , filepattern. on variable mappings section, have told container store file path value filepath variable.

  5. inside script task, replace main() method code code given under script task code section. there nothing fancy in code. displays file path in message box.

  6. screenshots #6 - #11 shows sample package execution , how each file in folder looped through. please note screenshot #11, script task marked green color stating package executed task.

  7. now, let's delete files in folder c:\temp shown in screenshot #12.

  8. if execute package now, tasks within foreach loop container not executed because there no files loop through , folder empty. refer screenshot #13. please note script task marked white color stating package didn't execute script task , skipped section.

  9. this simple example. can whole lot more display names of files. can have other tasks within foreach loop container , pass filepath variable process files.

hope helps.

script task code:

c# code can used in ssis 2008 , above.

public void main() {     variables varcollection = null;      dts.variabledispenser.lockforwrite("user::filepath");     dts.variabledispenser.getvariables(ref varcollection);      messagebox.show(varcollection["user::filepath"].value.tostring(), "file path");      dts.taskresult = (int)scriptresults.success; } 

screenshot #1:

1

screenshot #2:

2

screenshot #3:

3

screenshot #4:

4

screenshot #5:

5

screenshot #6:

6

screenshot #7:

7

screenshot #8:

8

screenshot #9:

9

screenshot #10:

10

screenshot #11:

11

screenshot #12:

12

screenshot #13:

13


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -