MY GOAL:
I want to use a data flow in a for loop container, where each iteration of the for loop sets the name of a database table in a string variable using an expression task, and then the data flow has an OLE DB Source that queries the table specified in the variable,
followed by other components that process the data.
WHAT I HAVE DONE:
I have created a data flow in SSIS, and the first component is an OLE DB Source. I have successfully used the component to retrieve data from a database table using a query such as the following:
SELECT col1, col2, col3
FROM table1
WHERE col4 = ?
In the parameter section of the OLE DB Source, I set the '?' to be a variable I set earlier in the SSIS package, and everything works great.
I have also placed a 'For Loop Container' in the control flow, and in the for loop I placed an 'Expression Task', followed by the data flow mentioned above. I created an integer variable name 'ForLoopCounter' to set the init, eval, and assign expressions as follows:
@ForLoopCounter = 0
@ForLoopCounter < 4
@ForLoopCounter = @ForLoopCounter + 1
MY PROBLEM:
I have a string variable named QueryVar, and I set it's value in the expression task to be "tableN", where N is the value of ForLoopCounter. I then try to use the following query in the OLE DB Source in the data flow:
SELECT col1, col2, col3
FROM ?
When I try to click on parameters I get the following error:
Parameters cannot be extracted from the SQL command. The provider might not help to parse parameter information from the command. In that case, use the "SQL command from variable" access mode, in which the entire SQL command is stored in a variable.
------------------------------
ADDITIONAL INFORMATION:
The batch could not be analyzed because of compile errors. (Microsoft SQL Server Native Client 11.0)
This error is confusing to me because I am able to specify a parameter in the where clause without any problems, but I am unable to use a parameter to specify the table name in the query.
I have also tried modifying the expression task to contain the entire SQL query, so I have the following:
QueryVal = "SELECT col1, col2, col3 FROM tableN" - once again, 'N' is replaced by a numerical value.
However, when I try to use the 'SQL command from variable' data access mode in the OLE DB Source, and provide QueryVal in the 'Variable Name' field, I get the following error when I click 'OK'.
Exception from HRESULT: 0xC0202009
Error at Data Flow - MyDataFlow [GetRawExtractData[19]]: SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has occurred. Error code: 0x80040E0C.
An OLE DB record is available. Source: "Microsoft SQL Server Native Client 11.0" Hresult: 0x80040E0C Description: "Command text was not set for the command object.".