Solved

Newbie - Nested If statements in stored procedure syntax?

Posted on 2008-10-28
3
2,557 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:
Daniel Reynolds 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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

786 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