Solved

Newbie - Nested If statements in stored procedure syntax?

Posted on 2008-10-28
3
2,555 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
Comment Utility
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
Comment Utility
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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 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

10 Experts available now in Live!

Get 1:1 Help Now