title: SSIS Object Variable populated in Foreach Loop set to last row outside of the loop, cannot access whole array
body: <p>I have an SSIS package that will assemble dynamic SQL statement and execute on a different server with the results needing to be written back to the first server.
  Because the SQL is created and passed in as a variable, a Foreach loop is used to run each instance.  The results are put into an Object Variable and This works fine.  If I put my script task in the Foreach loop itself, I can write the results back to the original server.  However- I would really like, for performance reasons, to get the insert out of the Foreach loop and read the result set / object variable to open one connection and write all the data at one go.  But when I pull the object doing the reading of the results and writing to the database out of the loop, it only write the last row of data, not all of them.
  How can I get to all the rows in the result set outside of the Foreach loop?  Is there a pointer to the first row or something?  I can't imagine I'm the first person to need to do this but my search for answers has come up empty.  Or maybe I'm just under caffeinated.
  <a href="https://i.stack.imgur.com/bOeh3.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bOeh3.jpg" alt="enter image description here" /></a></p>