Setting up the for-each loop was relatively simple, but I couldn't get the Execute SQL task to run a simple query of the type:
Select count(*) from @tableWhere @table is the name of the parameter generated from the for each loop.
It appears that this is not possible in SSIS. Eventually I found a solution: use another variable to store the SQL statement and set up the SQL task to use this variable as its input source.
Assuming that the input parameter is already set up and called [User::TableName], then the steps to achive this are:
- Set up a new variable (say called "SQLStatement"), make this a string type.
- In the properties box of the paremeter, Set EvaluateAsExpression to True
- In the properties box of the paremeter, click on the elipses (...) of the "Expression" property to build an expression. Type in the SQL you need and drag the variable you want to use from the "variables" tree.
- Escape you SQL in this expression wwithin quotation marks. e.g. the count statement above would look like:
- You can use the "Evaluate expression" button to make sure that this evaluates as real SQL. If you are using a "for-each" container then you may want to give your [User::TableName] variable a sensible default value in order to see the results of the evaluated expression in a format you could use for testing.
- Drag an "Execute SQL Task" object into your control flow and open its properties page
- Set the "SQL Source Type" property to "Variable" to tell it that the SQL will come from a variable, rather from a typed in SQL statement.
- Set the "SourceVariable" property to the name of the variable you created in step 1 (in this example it would be "SQLStatement".
"Select count(*) from " + @[User:TableName]