IF statement

The IF statement executes different sets of SQL statements based on the result of search conditions.

Syntax

>>-+--------+--IF--search-condition--THEN----------------------->
   '-label:-'                               

   .----------------------------.   
   V                            |   
>----SQL-procedure-statement--;-+------------------------------->

   .--------------------------------------------------------------------.   
   V                                                                    |   
>----+----------------------------------------------------------------+-+-->
     |                                 .----------------------------. |     
     |                                 V                            | |     
     '-ELSEIF--search-condition--THEN----SQL-procedure-statement--;-+-'     

>--+--------------------------------------+--END IF------------><
   |       .----------------------------. |           
   |       V                            | |           
   '-ELSE----SQL-procedure-statement--;-+-'           

Description

label
Specifies the label for the IF statement. The label name cannot be the same as the name of the SQL routine or another label name within the same scope. For additional information, see References to labels.
search-condition
Specifies the search-condition for which an SQL statement should be executed. If the condition is unknown or false, processing continues to the next search condition, until either a condition is true or processing reaches the ELSE clause.
SQL-procedure-statement
Specifies an SQL statement to be executed if the preceding search-condition is true. See SQL-procedure-statement.

Examples

Assign a value to the SQL variable new_salary based on the value of SQL variable rating.

IF rating = 1
 THEN SET new_salary =
  new_salary + (new_salary * .10);
 ELSEIF rating = 2
  THEN SET new_salary =
   new_salary + (new_salary * .05);
 ELSE SET new_salary =
  new_salary + (new_salary * .02);
END IF;