Solved

Newbie - Nested If statements in stored procedure syntax?

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MYSQL responding very slow 3 51
Need help in debugging a UDF results 7 55
SQL Server 2005 database messed up. Can it be fixed? 4 37
SSMS Imprt data from Excel 7 29
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…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…

730 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