Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Newbie - Nested If statements in stored procedure syntax?

Posted on 2008-10-28
Medium Priority
Last Modified: 2012-05-05
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.
Question by:bparkbpark
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 17

Accepted Solution

Daniel Reynolds earned 1000 total points
ID: 22827068
something like the following

   IF B
        SET x = x + 1
     SET y = y + 1
       IF C
             IF D
                    SET z = z + 1
              IF NOT D
                    Set z = z - 1
               SET ab = ab + 1
LVL 39

Assisted Solution

BrandonGalderisi earned 1000 total points
ID: 22827432
The important thing to note that there is no "end if" as in other languages.  And the use of "IF" means that if the condition is met, then execute the NEXT COMMAND.  If the NEXT COMMAND is a begin, then it treats that as a code block and executes until the matching END.

Let'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
set @b=2
set @c=3

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
  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
  set @b=123
  set @c=3

--end examples

Hope this clears up how to nest if/else statements

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question