3

I am writing to a csv file in a Foreach loop. For each loop the code is writing the header so it appears repeatedly throughout the file.

How do I ensure the header only appears once?

I have tried using RowCount variable and adding an expression to the Flat File Connection Manager but no change.

enter image description here

2

Since the link that I created in the comments did not contain this particular method, I figure I would throw it in here. Besides the approaches mentioned here I went ahead and tried this approach as another way that would take very little change to a package that is going through a ForEach Loop (since that is how I did it in mine). What you would do, is before running the ForEach Loop (and if you are doing the ForEach through an ADO recordset then you would want to do it before that) you could have a file kept in some other location that is nothing more than the file with the headers you want for this file and you would create a task (either Script task or a File System Task) to copy that file that you saved off to the location of the file you are generating via your ForEach Loop.

After you have done that, be certain that within your flat file connection that you have the "Column names in the first data row" unchecked.

If you do that, it will copy the file with the headers and then all of the rows added within your foreach loop will contain data only and no headers. It is an alternative if those others did not work for you.

Obviously it requires you to keep a file that exists only to be used for headers out on your server or some network drive but it is a solution that will get this done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.