SQL SERVER SCRIPT - NESTED IF STATEMENTS

I am attempting to figure out a SQL script and am unclear how nested IF statements behave. An example of what I am trying to figure out is shown below. Assuming that the 1st IF statement is true (A='22222'), does the last IF statement (IF D = 'ORTGNV')  get evaluated regardless of the answer to the second IF statement (IF B='12345' and C='98765')? I've also attached a word copy of the script in case indentation is messed up.

IF A = ‘22222’
      BEGIN
            DO AAA
            IF B = ‘12345’ and C = ‘98765’
                  BEGIN
                        DO BBB
                  END
            ELSE
                  BEGIN
                        DO CCC
                  END
            IF D = ‘ORTGNV’
                  BEGIN
                        DO DDD
                  END
END
SQL-Script-Nested-IF.docx
cbridgmanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vitor MontalvãoMSSQL Senior EngineerCommented:
Yes, the last IF will be evaluated no matter the values of B and C are. It only depends on A value (‘22222’).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>Assuming that the 1st IF statement is true (A='22222'), does the last IF statement (IF D = 'ORTGNV')  get evaluated regardless of the answer to the second IF statement (IF B='12345' and C='98765')?

Yes, as the first IF has a BEGIN..END block that includes the IF D test, and the IF B..C block has that IF with an ELSE, so either way code execution will continue down to the IF D line.

And in case you're wondering, IF statements only execute the very next line, and the way to get around that is to enclose multiple lines in an begin..end block.   So the begin's and end's involving b,c and d are really not necessary as they are executing only a single line.
0
Pawan KumarDatabase ExpertCommented:
IF D = ‘ORTGNV’ is executed IF A = ‘22222’.
IF A <> ‘22222’ Then nothing inside if is executed.
IF B = ‘12345’ and C = ‘98765’ THEN its begin part is executed.
IF NOT ( B = ‘12345’ and C = ‘98765’ ) Then its else part is executed.
0
cbridgmanAuthor Commented:
Thank you very much for your timely help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.