Use of variable with Boolean data Type in EvalExpression in For Loop container in SSIS - variables

I have a data flow task set in a For Loop Container. The loop should be executed for as long as the variable #LastPayment is not positive.
#LastPayment is a variable of DataType: Boolean, Value=False, EvaluateAsExpression=False and with the following set in Expression
"SELECT [Continue] FROM [DW_Test].[dbo].[Test]"
In my For Loop Editor, I have my EvalExpression set as :
#[LastPayment]="1"
When I run the SSIS package, the Loop does not stop even though SELECT [Continue] FROM [DW_Test].[dbo].[Test] gives return of 0 after a few iteration, hence #[LastPayment]="1" is FALSE after a few iteration. I do not understand why the ForLoop is not ending.
When I changed the EvalExpression to In my For Loop Editor to
#[LastPayment]=(DT_BOOL)"TRUE" or #[LastPayment]="TRUE", I get the same results.
When I changed the EvalExpression to In my For Loop Editor to #[LastPayment]="0"
No rows are generated.
Is there something wrong in my variable definition or the EvalExpression? Appreciate any advice on this. Thanks.

SSIS Booleans are not evaluated the same as SQL Server Bits. The way to test if a Boolean variable is true is like this:
#[LastPayment] == true

Just to update that I have resolved this.
Before the ForLoop process, I added a Execute SQL Task to determine the MaxLoop that the ForLoop process needs to go through and mapped the result set to variable #MaxLoop of datatype Int32.
In the ForLoop container, I added an expression task with expression #Loop=#Loop + 1. #Loop is a variable of datatype Int32. This is to count the number of loops the ForLoop has been processed.
My ForLoop EvalExpression is amended to #Loop<#MaxLoop.
Thanks for all the suggestions.

If you want to check logical expression in a For Loop, you should write it as #LastPayment<=0. Microsoft explains conditional expressions.

Related

SSIS: How do I pass string parameter in an update statement in Execute SQL Task?

I have one execute SQL Task in SSIS 2012 which has update statement. I want to pass a string variable in the where clause of this update statement. The update section is as below:
where coalesce(s1.iteration, '') not like '%?%' and s2.Iteration = '?'
Here, ? needs to be replaced with a string variable, which in this case would be 08152017. I have added the variable to the Parameter Mapping. Screenshot is attached.
The task executes successfully but does not updates the value in the intended column. It seems the query is not passing the value.What am I doing wrong? How do I check that the SQL inside the Execute SQL Task is actually getting the value from the variable?
First of all, when you set your variable in parameter mapping, make sure the datatype is NVARCHAR and not LONG.
Second you need to write your statement like this:
where coalesce(s1.iteration, '') not like '%?%' and s2.Iteration = ?
You dont need '' because your variable is already set as a string.
Try to hardcode your value in your variable to see if it passes. Otherwise, set a breakpoint on pre-execute to see wheter your variable has a value.
If your variable has a value and your SQL is not working, maybe you should look into your SQL. Maybe try it directly in SSMS to see if it actually runs or does anything.
In scenario ? is Integer type variable, then please use below format:
SELECT ? +' Hello World!'
The above does not require the use of an additional string variable.
Create a user variable with % on it, for example, variable name is Like_Var with data type String
"%" + #Orig_Var + "%"
Let's say, Orig_Var has a value of 08152017, therefore Like_Var will have %08152017%
Then use Like_Var on your parameter in Execute SQL Task as parameter 0, data type VARCHAR in Parameter Mapping
WHERE COALESCE(s1.iteration, '') NOT LIKE ?
AND s2.Iteration = ?

Evaluate a variable within a variable in Powershell

I have the following variables:
[string]$eth_netmask = "ext_netmask"
[string]$ext_netmask = "255.255.252.0"
$($eth_netmask) is returning ext_netmask. I was expecting 255.255.252.0
What am I doing wrong?
Thanks for the help in advance!
The command $eth_netmask returns the value of the variable named eth_netmask. The expression $(something) has nothing to do with variables, but instead evaluates the contents of the parentheses before evaluating the rest of the statement. That means that the statement $($eth_netmask) will evaluate in two steps:
1: $($eth_netmask) evaluates to the command "ext_netmask"
2: "ext_netmask" evaluates as a command which has the result of printing ext_netmask to the output.
This format is unnecessary since variables are normally resolved before the rest of the command anyway. My recommendation would be to avoid needing to do this at all if there is any alternative. Putting this kind of roundabout referencing into a piece of code can only cause problems. However, if you can't avoid it for some reason, it is possible to reference a variable the name of which is stored in another variable.
[string]$eth_netmask = "ext_netmask"
[string]$ext_netmask = "255.255.252.0"
Get-Variable -Name $eth_netmask -ValueOnly
This is the point at which the $(something) syntax becomes useful. If you need to use the value that you have just returned in another command, such as if the value was an ip that you were trying to ping, you might do something like this:
Test-Connection $(Get-Variable -Name $eth_netmask -ValueOnly)

Increment a variable in InstallAware

I've recently used InstallAware to convert my WISE project. It was not a great a success as many parts of the wise scripts were missing. Anyway, I was wondering if there is any option in InstallAware to increment a variable (to be used in a while loop). In WISE we could do so by setting the variable's operation as Increment.
Thanks,
you can easly accomplish to your request with the following code fragment in your InstallAware script:
Set Variable MYVAR to 0
while Variable MYVAR not Greater Than (Version) 9 loop
MYVAR = $MYVAR$ + 1
wend
I may suggest to have also a look to "Mathematics" embedded command.
Hope this helps you.

How to suppress VB's "Iteration variable shouldn't been used in lambda expression"

I'm working with LINQ in VB.NET and sometimes I get to a query like
For i = 0 To 10
Dim num = (From n In numbers Where n Mod i = 0 Select n).First()
Next
and then it comes the warning "Using the iteration variable in a lambda expression may have unexpected results. Instead, create a local variable within the loop and assign it the value of the iteration variable."
I know it's not a good practice to use the iteration variable in the lambda expression, because lambda expressions are evaluated only when needed. (This question is about that)
Now my question is, how to suppress this warning in cases where the expression is evaluated in-place, with constructions like First(), Single(), ToList(), etc. (It's only a warning, but i like my code clean.)
(Declaring a local variable and passing the iteration variable to it is an option, but I'm looking for a clean solution.)
In this particular case where the lambda is evaluated immediately, then you can safely eliminate the warning by moving the declaration of the iteration variable outside the for loop.
Dim i = 0
For i = 0 To 10
...
I do want to stress though that this only works if the lambda does not escape the for loop (true for your scenario).
Also here is a detailed link to an article I wrote on this warning (why it exists, how to avoid it, etc ...)
http://blogs.msdn.com/b/jaredpar/archive/2007/07/26/closures-in-vb-part-5-looping.aspx

one variable for all the sql output

myRs=myStmt.executeQuery("select i_col,col_name from tab_col")
i=0
while (myRs.next()):
list= myRs.getString("I_COL")+','+myRs.getString("COL_NAME")
i have a jython code to run a sql statement i want to store all the row of the sql into a single variable. I used to list to store the value but its always storing only the single line , so is there is way to append all the rows and keep adding to single variable.
Thanks so much for your help.
With that code you overwrite the "list" variable in every iteration of the while loop (= is an assignment), try something like this (I used rs rather than list to avoid a name clash with the builtin function list()):
myRs=myStmt.executeQuery("select i_col,col_name from tab_col")
rs=[]
i=0
while (myRs.next()):
rs.append(myRs.getString("I_COL")+','+myRs.getString("COL_NAME"))