?????????????????????BEGIN…END?????BEGIN…END?β????????DELIMITER($$)????????????????ú????;??β??????????????????????
???????????
??????洢?????????У???????ù??????????????????????????e?????????????OPEN??FETCH??CLOSE????????????£?
????<1>???????
????DECLARE cursor_name CURSOR FORselect_statement
????<2>?????
????OPEN cursor_name
????<3>??ù??
????FETCH cursor_name INTO var_name[??var_name]…
????<4> ?????
????CLOSE cursor_name
???????5????emp??????н????????????????deptno???????salary??????ж????????????????NOT FOUND????????FETCH?????????????????????????????????????
????DELIMITER $$
????CREATE PROCEDURE dept_salary_sum()
????BEGIN
????DECLAREi_dept_no int;
????DECLAREi_salary int(5);
????DECLAREcur_emp CURSOR FOR SELECT deptno??salary FROM emp;
????DECLAREEXIT HANDLER FOR NOT FOUND CLOSE cur_emp;
????SET@salary_sum_dept1=0;
????SET@salary_sum_dept2=0;
????OPENcur_emp;
????REPEAT
????FETCHcur_emp INTO i_dept_no??i_salary;
????IFi_dept_no=1 THEN
????SET@salary_sum_dept1=@salary_sum_dept1+i_salary;
????ELSE
????SET@salary_sum_dept2=@salary_sum_dept2+i_salary;
????ENDIF;
????UNTIL0 END REPEAT;
????CLOSEcur_emp;
????END $$
????DELIMITER ;
????CALL dept_salary_sum();
????SELECT @salary_sum_dept1??@salary_sum_dept2;
????->50000 27000
????????????????????????????????DECLARE?????????????????????????????????????????????????????????????????????????????????????????
???????????
???????????IF??CASE??LOOP??LEAVE??ITERATE??REPEAT??WHILE???????????????????????????ο?????汾???????5.6?汾??
????http://dev.mysql.com/doc/refman/5.6/en/flow-control-statements.html
????IF???
????IF search_condition THEN statement_list
????[ELSEIF search_condition THEN statement_list] ...
????[ELSE statement_list]
????END IF
????CASE???
????CASE case_value
????WHEN when_value THEN statement_list
????[WHEN when_value THEN statement_list] ...
????[ELSE statement_list]
????END CASE
????????
????CASE
????WHEN search_condition THEN statement_list
????[WHEN search_condition THEN statement_list] ...
????[ELSE statement_list]
????END CASE
????????????????У?IF???????????CASE?????????
????CASE
????WHEN i_dept_no =1 THEN
????SET@salary_sum_dept1=@salary_sum_dept1+i_salary;
????ELSE
????SET@salary_sum_dept2=@salary_sum_dept2+i_salary;
????END CASE;
?????????
????CASE i_dept_no
????WHEN 1 THEN
????SET@salary_sum_dept1=@salary_sum_dept1+i_salary;
????ELSE
????SET @salary_sum_dept2=@salary_sum_dept2+i_salary;
????END CASE;
????LOOP???
????[begin_label:] LOOP
????statement_list
????END LOOP [end_label]
????LOOP???????????????????????????????????????壬??????????LEAVE??????????????statement_list????????????????????LOOP???????????????????????
???????6????LOOP???
????CREATE PROCEDURE doiterate(p1 INT)
????BEGIN
????label1: LOOP
????SETp1 = p1 + 1;
????IFp1 < 10 THEN
????ITERATE label1;
????ENDIF;
????LEAVE label1;
????ENDLOOP label1;
????SET@x = p1;
????END;
????LEAVE???
????LEAVE label
?????????????????????????????????BEGIN…END????????????á?????????????
????ITERATE???
????ITERATE label
????ITERATE??????????????У?????????????????????μ????????????????????????????????
????REPEAT???
????[begin_label:] REPEAT
????statement_list
????UNTIL search_condition
????END REPEAT [end_label]
??????????????????????????????????????????????????5?????REPEAT????????????????á?