What would a nested if statement look like in a stored procedure:

01 If A

02 If B

03 Let x = x + 1

04 Endif B

05 Let y = y + 1

06 Else A

07 If C

08 If D

09 Let z = z + 1

09 Else D

10 Let z = z - 1

11 End D

12 Let ab = ab + 1

13 End C

14 End A

The original VBA code nests to 9 levels.

01 If A

02 If B

03 Let x = x + 1

04 Endif B

05 Let y = y + 1

06 Else A

07 If C

08 If D

09 Let z = z + 1

09 Else D

10 Let z = z - 1

11 End D

12 Let ab = ab + 1

13 End C

14 End A

The original VBA code nests to 9 levels.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.

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.

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 trialLet's look at these two examples.

In example 1, @b will ONLY be set to 2 if @a=1, but @c will be set to 3 regardless because the condition "if @a=1" only determines whether the "set @b=2" is executed

--example 1

If @A=1

set @b=2

set @c=3

In this example, both set operations will be executed because they exist inside of a begin/end block.

--example 2

if @A=1

begin

set @b=2

set @c=3

end

In this example, either @b will be set to 2, or @c will be set to 3. Both lines will not be executed.

--example 3

if @a=1

set @b=2

else

set @c=3

In this example. If @a=1, then @b will be set to 2. If @a is not equal to 1, then @b will be set to 123, and @c to 3.

--example 4

if @a=1

set @b=2

else

begin

set @b=123

set @c=3

end

--end examples

Hope this clears up how to nest if/else statements

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.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.

IF A

BEGIN

IF B

BEGIN

SET x = x + 1

END

SET y = y + 1

END

IF NOT A

BEGIN

IF C

BEGIN

IF D

BEGIN

SET z = z + 1

END

IF NOT D

BEGIN

Set z = z - 1

END

SET ab = ab + 1

END

END