I'm trying to create an SSIS package that will loop through a series of SQL expressions using variables. The variables to be set in SSIS would come from a SQL table, something similar to below.
| Var1 | Var2 | Var3 | Var4 |
|---|---|---|---|
| NOV | 2022 | ABC | 1 |
| DEC | 2022 | EFG | 2 |
| JAN | 2023 | HIJ | 1 |
I know that to incorporate SSIS variables into SQL statements I have to create an Execute SQL Task and set an expression for the SQLStatementSource that uses designated SSIS variables. What I'm less certain of is how to iterate through the table variables while within a ForEach loop.
An Example of what the Executed SQL task should look like would be something like...
SELECT [MONTH], [YEAR], [Code], [Level], *
FROM TableSource
WHERE [MONTH] like '[User::Var1]'
AND [YEAR] like '[User::Var2]'
AND [Code] like '[User::Var3]'
AND [Level] like '[User::Var4]'
For each row within the variable table selected originally, the variables within the statement should use all designated values across that row. Then, after performing the statement, it should loop back and continue the container using the next row's values for the variables and complete the same statement until the variable table has been iterated to completion.
A "where matches any of X" condition can often be coded up as "where exists(X where X matches)".
Try:
A join may be used for a similar effect, which might actually allow for a more efficient use of indexes.
Add a DISTINCT if there is a possibility of a TableSource row matching more than one criteria row.
If you really want a loop with each executing a separate select, you can use a
CURSOR LOOP.