Mysql那些事兒之(十五)流程的控制
相關鏈接:
Mysql那些事兒之(一)mysql的安裝
http://www.BkJia.com/database/201210/162314.html;
Mysql那些事兒之(二)有關數據庫的操作
http://www.BkJia.com/database/201210/162315.html;
Mysql那些事兒之(三)有關數據表的操作
http://www.BkJia.com/database/201210/162316.html;
Mysql那些事兒之(四)數據表數據查詢操作
http://www.BkJia.com/database/201210/162317.html;
Mysql那些事兒之(五)操作時間
http://www.BkJia.com/database/201210/162318.html;
Mysql那些事兒之(六)字符串模式匹配
http://www.BkJia.com/database/201210/163969.html;
Mysql那些事兒之(七)深入select查詢
http://www.BkJia.com/database/201210/163970.html;
Mysql那些事兒之(八)索引
http://www.BkJia.com/database/201210/163971.html;
Mysql那些事兒之(九)常用的函數
http://www.BkJia.com/database/201210/164229.html;
Mysql那些事兒之(十)觸發器一
http://www.BkJia.com/database/201210/164516.html;
Mysql那些事兒之(十一)觸發器二
http://www.BkJia.com/database/201210/164766.html;
Mysql那些事兒之(十二)存儲過程
http://www.BkJia.com/database/201210/164795.html;
Mysql那些事兒之(十三)變量、條件的使用
http://www.BkJia.com/database/201211/165662.html;
Mysql那些事兒之(十四)光標的使用
http://www.BkJia.com/database/201211/165664.html
IF語句 www.2cto.com
Sql代碼
---語法結構
IF search_condition THEN statement_list
[ELSEIF search_condition THEN statement_list]....
[ELSE statement_list]
END IF
---舉例
if i_staff_id = 2 then
set @x1 = @x1 + d_amount;
else
set @x2 = @x2 + d_amount;
end if;
CASE語句
Sql代碼
---CASE語句的語法格式
CASE case_value
WHEN when_value THEN statement_list
[WHEN when_value THEN statement_list]....
[ELSE statement_list]
END CASE
www.2cto.com
---case語句舉例:
case
when i_staff_id = 2 then
set @x1 = @x1 + d_amount;
else
set @x2 = @x2 + d_amount;
end case
LOOP語句
Sql代碼
[begin_label:] LOOP
statement_list
END LOOP [end_label]
---如果不在statement_list中增加退出循環的語句,那麼LOOP語句可以用來實現簡單的死循環。
LEAVE語句
Sql代碼
---將結束符替換為$$
delimiter $$
---創建存儲過程
CREATE PROCEDURE actor_num()
BEGIN
set @x = 0;
ins:LOOP
set @x = @x + 1;
IF @x = 100 THEN
leave ins;
END IF;
INSERT INTO actor(first_name,last_name) VALUES('TEST',222);
END LOOP ins;
END;
$$
delimiter ;
ITERATE語句
Sql代碼
--必須用在循環中,作用就是跳過當前的循環直接進入下一輪循環。
delimiter $$
CREATE PROCEDURE actor_num()
BEGIN
set @x = 0;
ins:LOOP
set @x = @x + 1;
IF @x = 100 THEN
leave ins;
ELSEIF mod(@x/2,0) = 0 THEN
iterate ins;
END IF;
INSERT INTO actor(first_name,last_name) VALUES('TEST',222);
END LOOP ins;
END;
$$
delimiter ;
REPEAT 語句
Sql代碼
--有條件循環,當滿足條件的時候退出循環。
--語法:
[begin_label:] REPEAT
statement_list
UNTIL search_condition
END REPEAT [end_label]
--舉例
REPEAT
FETCH cur_payment INTO i_staff_id,d_amount;
if i_staff_id = 2 then
set @x1 = @x1 + d_amount;
else
set @x2 = @x2 + d_amount;
end if;
UNTIL 0 END REPEAT;
WHILE 語句
Sql代碼
---語法結構
[begin_label:] WHILE search_condition DO
statement_list
END WHILE [end_label]