MySQL存储过程之代码块、条件控制、迭代

1,代码块
代码顺序

 

1 ,变量和条件声明   
2 ,Cursor声明   
3 ,Handler声明   
4 ,程序代码  

 

可以给代码块加lebel,这样END匹配比较直观,还可以用LEAVE语句来终结代码块:

[ label: ]   BEGIN    
    varaiable 
and  condition declarations   
    
cursor  declarations   
    handler declarations   
  
    program code   
END   [ label ] ;  

 

代码块可以嵌套:

ExpandedBlockStart.gif 代码
CREATE   PROCEDURE  nested_blocks()   
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的例子:

 

ExpandedBlockStart.gif 代码
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:

IF  expression  THEN  commands   
    
[ ELSEIF expression THEN commands ]    
    
[ ELSE commands ]    
END   IF

 

例子:

ExpandedBlockStart.gif 代码
IF  (sale_value  >   200 THEN    
    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:

CASE    
    
WHEN  condition  THEN    
        statements   
    
[ WHEN condition THEN   
        statements...
]    
    
[ ELSE   
        statements
]    
END   CASE ;  

 

例子:

ExpandedBlockStart.gif 代码
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语句结合的妙用:

SELECT  ( CASE   WHEN  (t.a  =   1   AND  t.b  =   0 THEN  t.c  ELSE   ' N/A '   END AS  result  FROM  test t  order   by  result  asc   

 

3,迭代
LOOP

[ label: ]  LOOP   
    statements   
END  LOOP  [ label ] ;  

 


REPEAT...UNTIL

[ label: ]  REPEAT   
    statements   
UNTIL expression   
END  REPEAT  [ label ]   

 

WHILE

[ label: ]   WHILE  expression DO   
    statements   
END   WHILE   [ label ]   

 

LEAVE语句

SET  i = 1 ;   
myloop: LOOP   
    
SET  i = i + 1 ;   
    
IF  i = 10   then    
        LEAVE myloop;   
    
END   IF :   
END  LOOP myloop;   
SELECT   ' I can count to 10 ' ;  

 


ITERATE语句

ExpandedBlockStart.gif 代码
SET  i = 0 ;   
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;  

 


嵌套循环

ExpandedBlockStart.gif 代码
DECLARE  i, j  INT   DEFAULT   1 ;   
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;  

 


 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值