Flow Control Constructs
Flow Control Constructs include the IF, CASE, LOOP, WHILE, ITERATE, REPEAT and LEAVE constructs. They are fully implemented.These constructs can contain single statement or a block of statements using with BEGIN?..END statement. And these constructs can be nested also.
IF Statement
The general syntax of IF Statement is :
IF search_condition THEN statement_list [ELSEIF search_condition THEN statement_list] ...[ELSE statement_list] END IF
IF statement implements a basic conditional construct. When search_condition is true then only corresponding SQL statement_list is executed but if it is false then the ELSE clause statement_list is executed. statement_list can consists one or more statements. Example :
mysql> delimiter // mysql> delimiter ; mysql> CALL IFProc(8); |
mysql> SELECT Name AS NAME, City AS CITY, |
The general syntax of CASE Statement is :
CASE case_value WHEN when_value THEN statement_list [WHEN when_value THEN statement_list] ... [ELSE statement_list] END CASEOr CASE WHEN search_condition THEN statement_list [WHEN search_condition THEN statement_list] ... [ELSE statement_list] END CASE
The first syntax execute the statement_list when the case_value=when_value. If there is no matching case_value with when_value then it execute the ELSE clause statement_list. And in the second syntax, if search_condition is true then only corresponding SQL statement_list is execute but if it is false then the ELSE clause statement_list is executed. Example :
mysql> delimiter // mysql> delimiter ; mysql> CALL WHENProc(3); mysql> CALL WHENProc(1); |
The general syntax of LOOP Statement is :
[begin_label:] LOOP statement_list END LOOP [end_label]
LOOP Statement implements a simple loop construct. This statement is used to repeat execution of the statement_list, statement_list can contain one or more than one statements. These statements can repeat the execution until the loop is exited and usually that can be done with a LEAVE Statement. The LOOP Statement can be labeled also.
LEAVE Statement
The general syntax of LEAVE Statement is :
LEAVE label
The LEAVE Statement is used to exit from any flow control constructs.
In the following example we are describing you a both LOOP and LEAVE Statement.
mysql> delimiter // mysql> delimiter ; +------+---------------+--------+ +------+-----------+----------+ +------+-----------+---------+ +------+---------+-------+ Query OK, 0 rows affected (0.04 sec) |
The general syntax of ITERATE Statement is:
ITERATE label
ITERATE Statement can appear only within REPEAT, LOOP and WHILE Statements. ITERATE is used to iterate (Continue) the loop again. Example :
mysql> delimiter // mysql> delimiter ; |
The general syntax of REPEAT Statement is:
[begin_label:] REPEAT statement_list UNTIL search_condition END REPEAT [end_label]
Statement_list contains the one or more statements. REPEAT Statement is used to repeat the statement_list until the search_condition evaluates true. The REPEAT Statement can be labeled also.Example :
mysql> delimiter // mysql> delimiter ; +---------+-------------+------+----------+ +---------+-------------+------+--------+ Empty set (0.04 sec) +---------+-------------+------+----------+ |
The general syntax of WHILE Statement is:
[begin_label:] WHILE search_condition DO statement_list END WHILE [end_label]
The WHILE Statement repeats the statement_list until the search_condition evaluates true. The WHILE Statement can be labeled also. Example :
|
No comments:
Post a Comment