Solved

Newbie - Nested If statements in stored procedure syntax?

Posted on 2008-10-28
3
2,556 Views
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.
0
Comment
Question by:bparkbpark
3 Comments
 
LVL 17

Accepted Solution

by:
xDJR1875 earned 250 total points
ID: 22827068
something like the following

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
0
 
LVL 39

Assisted Solution

by:BrandonGalderisi
BrandonGalderisi earned 250 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
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
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

912 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now