Real World Foreach Loop Container example

  • Comments posted to this topic are about the item Real World Foreach Loop Container example

  • Great article. I was just about to send a very similar article for submission. Guess you beat me to it:)

  • I second that. The for each loops are a great tool. I use one with variables and script for looping through database backups on my main server to grab the latest dated ones and FTP them over to a secondary backup server (no I'm not really paranoid about losing data!)

    Best Regards

  • Sorry 😉

  • Just came across your article and fine it very useful tools for me to learn sqlis. I have a problem with Execute SQL Task in the loop container:

    Insert into tmpTlogFileList(FileName)values(@FileName):

    Execute SQL Task: Executing the query "insert into tmpTlogFileList (FileName) values (@FileName)" failed with the following error: "Must declare the scalar variable "@FileName".".

    Must I declare @Filename as nvarchar(128) from the sql statement?


  • Right-click your "Execute SQL Task". In the "Execute SQL Task Editor", go to Parameter Mapping. Add a parameter as follows:

    Variable Name - User::varTLogFileName

    Direction - Input

    Data Type - String

    Parameter Name - @FileName

    Parameter Size: -1

    Hope this is now clear. Best of luck!

  • Paul,

    I get the same error...Here are a bit more detail on execute sql task:

    General tab:

    resultset: None

    connectionType: OLE DB

    connection: UserServerConnection

    sqlsourceType: Direct input

    sqlstatement: insert into tmpTLogFileList(FileName) values (@FileName)

    bypassPrepare: True

    The Parameter Mapping is same above as you mention, except

    for Data Type - Nvarchar, I can't fine the dropdown for String.

    I appreciate all you help thanks....

    Error: 0xC002F210 at Execute SQL Task, Execute SQL Task: Executing the query "insert into tmpTLogFileList(FileName) values (@FileName)" failed with the following error: "Must declare the scalar variable "@FileName".". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.

    Task failed: Execute SQL Task

  • tran008,

    I think you need to provide a bit more basic information about what you are trying to do. Where are you getting the @FileName parameter from anyway? My thoughts are that it should be declared as a User::[varname] in the for each loop and then this mapped across to the parameter in the SQL Task, but without knowing what it is you are trying to do it is hard to be of more assistance.

    Best Regards

  • As an after thought - depending on what you wish to do of course, you may have to execute the sql statement as a variable. Out of curiosity I just put together a for each loop to save a series of .txt files into a table and this worked well by using a variable for the sql statement. Let me know if you want any further detail on this method.

    Best Regards

  • thanks...I finally figure this out after long search.


    INSERT INTO tempTlogFileList


    VALUES (?)

    instead of

    INSERT INTO tempTlogFileList


    VALUES (@FileName)

  • Sorry I couldn't get back to you earlier on this and save you the long search, but the cause of your problem was the OLEDB connection. My example uses an ADO.NET connection for the Execute SQL Task (this wasn't explicitly mentioned in the article - my apologies), in which case you need to use the (@FileName) syntax as described on the article.

  • Oh, good, the (?) parameter works. You still have to do the Parameter Mapping in the Execute SQL task (the parameter name = 0).


  • I'm new to SSIS so this might be a very basis error but I'd appreciate some


    I've got error in Delete Old log File (File system Task) :

    Variable VarDeleteFileSName is used as a source or destination and is empty

    Please help me..

    Thank you

  • You'll need to do a bit of debugging. The first place I'd look is in the SQL statement that populates the varDeleteFileList variable; if you run this as a query in the Management Studio do you get a result set? If not, that's your problem.

    If you do, try adding breakpoints in the script code just before the Dts.TaskResult line (for instance). That way you'll be able to see which (if any) values are being passed along.

  • Paul - Just wanted to thank you for putting this "Real World" article up. I'm working on adapting an existing full-blown ETL/OLAP iterative process to use SSIS looping tasks to provide high-level control of detail low-level SP processing. The details of your Var defs, DeleteList DataFlow, and Convert Object variable script were just what I was looking for - thanks for taking the time to do this.

    -- BobS --

Viewing 15 posts - 1 through 15 (of 24 total)

You must be logged in to reply to this topic. Login to reply