IF statements and order

Post questions here relative to DataStage Server Edition for such areas as Server job design, DS Basic, Routines, Job Sequences, etc.

Moderators: chulett, rschirm, roy

Post Reply
justlrng
Participant
Posts: 30
Joined: Thu Oct 16, 2003 1:17 pm

IF statements and order

Post by justlrng »

I am just wondering if anyone has ever had an issue with the IF statement and the ordering of the clauses within the IF statement. On several occassions, I have tried to debug some of my jobs and the problem ended up being the order of the comparisons I was making in an IF statement.

For example

If TRIM(olePersonRoleIn.tchr_ref_nr) <> TRIM(UpdateTchrRefNr) OR TRIM(olePersonRoleIn.tchr_cert_nr) <> TRIM(UpdateTchrCertNr) OR TRIM(olePersonRoleIn.ait_id) <> TRIM(UpdateAITId) OR olePersonRoleIn.ab_ed_id <> UpdateASN OR olePersonRoleIn.deceased_fl <> UpdateDeceasedFl Or olePersonRoleIn.given_nm <> UpdateGivenNm Or olePersonRoleIn.middle_nm <> UpdateMiddleNm Or olePersonRoleIn.sur_nm <> UpdateSurNm Or olePersonRoleIn.person_nm_title_ty <> UpdateNmTitleTy Or olePersonRoleIn.person_role_ty <> UpdatePersonRoleTy Or olePersonRoleIn.protected_fl <> If UpdateProtectedFl = "U" Then olePersonRoleIn.protected_fl Else UpdateProtectedFl Then "T" Else "F"

this statement works but if I order the comparisons in a slightly different way,

If TRIM(olePersonRoleIn.tchr_ref_nr) <> TRIM(UpdateTchrRefNr) OR TRIM(olePersonRoleIn.tchr_cert_nr) <> TRIM(UpdateTchrCertNr) OR olePersonRoleIn.ab_ed_id <> UpdateASN OR olePersonRoleIn.deceased_fl <> UpdateDeceasedFl Or olePersonRoleIn.given_nm <> UpdateGivenNm Or olePersonRoleIn.middle_nm <> UpdateMiddleNm Or olePersonRoleIn.sur_nm <> UpdateSurNm Or olePersonRoleIn.person_nm_title_ty <> UpdateNmTitleTy Or olePersonRoleIn.person_role_ty <> UpdatePersonRoleTy Or olePersonRoleIn.protected_fl <> If UpdateProtectedFl = "U" Then olePersonRoleIn.protected_fl Else UpdateProtectedFl OR TRIM(olePersonRoleIn.ait_id) <> TRIM(UpdateAITId) Then "T" Else "F"


then I find this evaluates to "T" when I expect "F".

As I stated, this is not the first time that this has happened and am curious to know if anyone else has experienced similiar problems and if there is a solution to this.

Thanks
Last edited by justlrng on Thu Apr 07, 2005 10:08 am, edited 1 time in total.
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

You might want to explain what "if I order the comparisons in a slightly different way" means exactly. An example would be good. :wink:
-craig

"You can never have too many knives" -- Logan Nine Fingers
ArndW
Participant
Posts: 16318
Joined: Tue Nov 16, 2004 9:08 am
Location: Germany
Contact:

Post by ArndW »

Justlrng,

You are getting an error of your OR's evaluating to true when you think they shouldn't; so [in addition to putting in some parenthesis first for clarity] you can remove one condition at a time until you get the expected row result, then you've found the culprit. With the data there isn't too much for us to do here.

The conditions are evaluated left-to-right but no optimization is done (i.e. the evaluation does not stop when the first true is found, even if all conditions are OR'd together)
PhilHibbs
Premium Member
Premium Member
Posts: 1044
Joined: Wed Sep 29, 2004 3:30 am
Location: Nottingham, UK
Contact:

Re: IF statements and order

Post by PhilHibbs »

justlrng wrote:I am just wondering if anyone has ever had an issue with the IF statement and the ordering of the clauses within the IF statement. On several occassions, I have tried to debug some of my jobs and the problem ended up being the order of the comparisons I was making in an IF statement.
Try putting some parentheses round the embedded if:

If TRIM(olePersonRoleIn.tchr_ref_nr) <> TRIM(UpdateTchrRefNr) OR TRIM(olePersonRoleIn.tchr_cert_nr) <> TRIM(UpdateTchrCertNr) OR TRIM(olePersonRoleIn.ait_id) <> TRIM(UpdateAITId) OR olePersonRoleIn.ab_ed_id <> UpdateASN OR olePersonRoleIn.deceased_fl <> UpdateDeceasedFl Or olePersonRoleIn.given_nm <> UpdateGivenNm Or olePersonRoleIn.middle_nm <> UpdateMiddleNm Or olePersonRoleIn.sur_nm <> UpdateSurNm Or olePersonRoleIn.person_nm_title_ty <> UpdateNmTitleTy Or olePersonRoleIn.person_role_ty <> UpdatePersonRoleTy Or olePersonRoleIn.protected_fl <> (If UpdateProtectedFl = "U" Then olePersonRoleIn.protected_fl Else UpdateProtectedFl) Then "T" Else "F"

Personally, I would have made that embedded if into a stage variable (or just a regular variable if this is in a routine not a derivation).

Sorry, my mistake, I misunderstood your post. It's the second one that's going wrong, isn't it? Well, the parentheses will fix that. Your code as posted is being executed as this:

If TRIM(olePersonRoleIn.tchr_ref_nr) <> TRIM(UpdateTchrRefNr) OR TRIM(olePersonRoleIn.tchr_cert_nr) <> TRIM(UpdateTchrCertNr) OR olePersonRoleIn.ab_ed_id <> UpdateASN OR olePersonRoleIn.deceased_fl <> UpdateDeceasedFl Or olePersonRoleIn.given_nm <> UpdateGivenNm Or olePersonRoleIn.middle_nm <> UpdateMiddleNm Or olePersonRoleIn.sur_nm <> UpdateSurNm Or olePersonRoleIn.person_nm_title_ty <> UpdateNmTitleTy Or olePersonRoleIn.person_role_ty <> UpdatePersonRoleTy Or olePersonRoleIn.protected_fl <> If UpdateProtectedFl = "U" Then olePersonRoleIn.protected_fl Else (UpdateProtectedFl OR TRIM(olePersonRoleIn.ait_id) <> TRIM(UpdateAITId)) Then "T" Else "F"

It seems that OR binds with a higher precedence than ELSE. Always always always clarify your expressions with parentheses.
Phil Hibbs | Capgemini
Technical Consultant
kduke
Charter Member
Charter Member
Posts: 5227
Joined: Thu May 29, 2003 9:47 am
Location: Dallas, TX
Contact:

Post by kduke »

Do it in a routine. Nested case statements would be lots easier to read and modify later.
Mamu Kim
justlrng
Participant
Posts: 30
Joined: Thu Oct 16, 2003 1:17 pm

Post by justlrng »

Thanks for all your help. It seems to be just an issue that I have. What I have found also works is to place portions of the statement in yet another stage variable and evaluate it there.

Thanks.
Post Reply