I have a row count component that uses a global variable X to store the number; up to this point it rules but then if I add a script component that has X among its ReadOnly variables and then use it inside it for some purpose, the flow hangs on for a moment then it crashes saying row count needs a valid variable.
Isn’t it possible to store the row count in a variable and then read this variable from a script comp. ?
You can not use a variable you populate in a Row Count transformation anywhere else in that same data flow. In the PreExecute phase of the data flow, the Row Count locks the variable. Attempting to use the variable in a Script component would have the Script try the same thing (at the same time) and SSIS therefore throws an error.
But even if that didn’t happen, here’s what else is going on. As your data flow works, and rows pass into the Row Count, it isn’t actually filling the SSIS variable with the row count. That would be very inefficient, since the Row Count is actually a COM component and the SSIS variables are .Net. It’s “expensive” to use .Net resources from COM and vice-versa. So the Row Count accumulates the value in a local code variable. In the PostExecute phase, the Row Count then pushes the value it collected into the SSIS variable. The key point is that the PostExecute phase happens after the data flow is complete. If you were able to view the variable at any time during the middle of the data flow (there are ways) you would see it valued at zero (or whatever value it was set to before the data flow started).
If you want to use a count of rows to perform a conditional action, or update rows within the flow, you have a couple choices: