0% found this document useful (0 votes)
40 views2 pages

13.6.5.2. Syntax: Section 12.4, "Control Flow Functions"

The IF statement implements conditional logic in stored programs. It allows for a THEN clause which executes if the search_condition is true, and optional ELSEIF and ELSE clauses which execute if the search_condition is false. The IF statement must end with END IF and can contain nested IF statements with each ending in its own END IF.

Uploaded by

Agus Oka Gunawan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views2 pages

13.6.5.2. Syntax: Section 12.4, "Control Flow Functions"

The IF statement implements conditional logic in stored programs. It allows for a THEN clause which executes if the search_condition is true, and optional ELSEIF and ELSE clauses which execute if the search_condition is false. The IF statement must end with END IF and can contain nested IF statements with each ending in its own END IF.

Uploaded by

Agus Oka Gunawan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

13.6.5.2.

IF Syntax
IF search_condition THEN statement_list [ELSEIF search_condition THEN statement_list] ... [ELSE statement_list] END IF

The IF statement for stored programs implements a basic conditional construct.


Note

There is also an IF() function, which differs from the IF statement described here. See Section 12.4, Control Flow Functions. The IF statement can have THEN, ELSE, and ELSEIF clauses, and it is terminated with END IF. If the search_condition evaluates to true, the corresponding THEN or ELSEIF clause statement_listexecutes. If no search_condition matches, the ELSE clause statement_list executes. Each statement_list consists of one or more SQL statements; an empty statement_list is not permitted. An IF ... END IF block, like all other flow-control blocks used within stored programs, must be terminated with a semicolon, as shown in this example:
DELIMITER // CREATE FUNCTION SimpleCompare(n INT, m INT) RETURNS VARCHAR(20) BEGIN DECLARE s VARCHAR(20); IF n > m THEN SET s = '>'; ELSEIF n = m THEN SET s = '='; ELSE SET s = '<'; END IF; SET s = CONCAT(n, ' ', s, ' ', m); RETURN s; END // DELIMITER ;

As with other flow-control constructs, IF ... END IF blocks may be nested within other flowcontrol constructs, including other IF statements. Each IF must be terminated by its own END
IF followed by a semicolon. You can use indentation to make nested flow-control blocks more

easily readable by humans (although this is not required by MySQL), as shown here:
DELIMITER //

CREATE FUNCTION VerboseCompare (n INT, m INT) RETURNS VARCHAR(50) BEGIN DECLARE s VARCHAR(50); IF n = m THEN SET s = 'equals'; ELSE IF n > m THEN SET s = 'greater'; ELSE SET s = 'less'; END IF; SET s = CONCAT('is ', s, ' than'); END IF; SET s = CONCAT(n, ' ', s, ' ', m, '.'); RETURN s; END // DELIMITER ;

In this example, the inner IF is evaluated only if n is not equal to m.

You might also like