MySQL存储过程之代码块、条件控制、迭代
生活随笔
收集整理的這篇文章主要介紹了
MySQL存储过程之代码块、条件控制、迭代
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1,代碼塊
代碼順序
?
1,變量和條件聲明???2,Cursor聲明???
3,Handler聲明???
4,程序代碼??
?
可以給代碼塊加lebel,這樣END匹配比較直觀,還可以用LEAVE語句來終結代碼塊:
????varaiable?and?condition?declarations???
????cursor?declarations???
????handler?declarations???
??
????program?code???
END?[label];??
?
代碼塊可以嵌套:
BEGIN???
????DECLARE?my_variable?varchar(20);???
????SET?my_variable='This?value?was?set?in?the?outer?block';???
????BEGIN???
????????SET?my_variable='This?value?was?set?in?the?inner?block';???
????END;???
????SELECT?my_variable,?'Changes?in?the?inner?block?are?visible?in?the?outer?block';???
END;?
?
LEAVE的例子:
?
代碼 CREATE?PROCEDURE?nested_blocks()???outer_block:?BEGIN???
????DECLARE?l_status?int;???
????SET?l_status=1;???
????inner_block:?BEGIN???
????????IF?(l_status=1)?THEN???
????????????LEAVE?inner_block;???
????????END?IF;???
????????SELECT?'This?statement?will?never?be?executed';???
????END?inner_block;???
????SELECT?'End?of?program';???
END?outer_block;??
?
2,條件控制
IF:
????[ELSEIF?expression?THEN?commands]???
????[ELSE?commands]???
END?IF;?
?
例子:
????CALL?free_shipping(sale_id);????/*Free?shipping*/??
????IF?(customer_status='PLATINUM')?THEN???
????????CALL?apply_discount(sale_id,20);?/*?20%?discount?*/??
????ELSEIF?(customer_status='GOLD')?THEN???
????????CALL?apply_discount(sale_id,15);?/*?15%?discount?*/??
????ELSEIF?(customer_status='SILVER')?THEN???
????????CALL?apply_discount(sale_id,10);?/*?10%?discount?*/??
????ELSEIF?(customer_status='BRONZE')?THEN???
????????CALL?apply_discount(sale_id,5);?/*?5%?discount*/??
????END?IF;???
END?IF;??
?
CASE:
????WHEN?condition?THEN???
????????statements???
????[WHEN?condition?THEN???
????????statements...]???
????[ELSE???
????????statements]???
END?CASE;??
?
例子:
????WHEN?(sale_value>200)?THEN???
????????CALL?free_shipping(sale_id);???
????????CASE?customer_status???
????????????WHEN?'PLATINUM'?THEN???
????????????????CALL?apply_discount(sale_id,20);???
????????????WHEN?'GOLD'?THEN???
????????????????CALL?apply_discount(sale_id,15);???
????????????WHEN?'SILVER'?THEN???
????????????????CALL?apply_discount(sale_id,10);???
????????????WHEN?'BRONZE'?THEN???
????????????????CALL?apply_discount(sale_id,5);???
????????END?CASE;???
END?CASE;??
?
CASE與SELECT語句結合的妙用:
?
3,迭代
LOOP
????statements???
END?LOOP?[label];??
?
REPEAT...UNTIL
????statements???
UNTIL?expression???
END?REPEAT?[label]??
?
WHILE
????statements???
END?WHILE?[label]??
?
LEAVE語句
myloop:?LOOP???
????SET?i=i+1;???
????IF?i=10?then???
????????LEAVE?myloop;???
????END?IF:???
END?LOOP?myloop;???
SELECT?'I?can?count?to?10';??
?
ITERATE語句
loop1:?LOOP???
????SET?i=i+1;???
????IF?i>=10?THEN?????????????????/*Last?number?-?exit?loop*/??
????????LEAVE?loop1;???
????ELSEIF?MOD(i,?2)=0?THEN???????/*Even?number?-?try?again*/??
????????ITERATE?loop1;???
????END?IF;???
??
????SELECT?CONCAT(i,?"?is?an?odd?number");???
END?LOOP?loop1;??
?
嵌套循環
outer_loop:?LOOP???
????SET?j=1;???
????inner_loop:?LOOP???
????????SELECT?concat(i,?"?times?",?j,?"?is?",?i*j);???
????????SET?j=j+1;???
????????IF?j>12?THEN???
????????????LEAVE?inner_loop;???
????????END?IF;???
????END?LOOP?inner_loop;???
????SET?i=i+1;???
????IF?i>12?THEN???
????????LEAVE?outer_loop;???
????END?IF;???
END?LOOP?outer_loop;??
?
?
?
?
總結
以上是生活随笔為你收集整理的MySQL存储过程之代码块、条件控制、迭代的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何添加sersync进程监控脚本
- 下一篇: linux cmake编译源码,linu