same name, MySQL currently interprets the reference as the name of a variable. Consider the following procedure definition: 

CREATE PROCEDURE sp1 (x VARCHAR(5)) BEGIN DECLARE xname VARCHAR(5) DEFAULT ’bob’; DECLARE newname VARCHAR(5); DECLARE xid INT; SELECT xname, id INTO newname, xid FROM table1 WHERE xname = xname; SELECT newname; END; 

MySQL interprets xname  in the SELECT  statement as a reference to the xname variable  rather than the xname column . Consequently, when the procedure sp1() is called, the newname  variable returns the value ’bob’  regardless of the value of the table1.xname  column. 

Similarly, the cursor definition in the following procedure contains a SELECT  statement that refers to xname . MySQL interprets this as a reference to the variable of that name rather than a column reference. 

## 13.6.5 Flow Control Statements 

CREATE PROCEDURE sp2 (x VARCHAR(5)) BEGIN DECLARE xname VARCHAR(5) DEFAULT ’bob’; DECLARE newname VARCHAR(5); DECLARE xid INT; DECLARE done TINYINT DEFAULT 0; DECLARE cur1 CURSOR FOR SELECT xname, id FROM table1; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; OPEN cur1; read_loop: LOOP FETCH FROM cur1 INTO newname, xid; IF done THEN LEAVE read_loop; END IF; SELECT newname; END LOOP; CLOSE cur1; END; 

See also Section 25.8, “Restrictions on Stored Programs” . 

MySQL supports the IF , CASE , ITERATE , LEAVE LOOP , WHILE , and REPEAT  constructs for flow control within stored programs. It also supports RETURN  within stored functions. 

Many of these constructs contain other statements, as indicated by the grammar specifications in the following sections. Such constructs may be nested. For example, an IF  statement might contain a WHILE loop, which itself contains a CASE  statement. 

## 13.6.5.1 CASE Statement 

MySQL does not support FOR  loops. 

CASE case_value WHEN when_value  THEN statement_list [WHEN when_value  THEN statement_list ] ... [ELSE statement_list ] END CASE 

Or: 

