mysql存储过程中的三种循环

#loop
drop procedure if exists p1_;
create procedure p1_ ( in in_  int ,  out out_ varchar(3)) #varchar 必须指定长度,传入参数时只能比3短,不能多
begin 
declare variable_ int(2);
set  variable_ = 10;

looplabel : loop

select variable_, in_ ,out_ from dual;

set variable_ = variable_ - 1;
if variable_ < 6 then
set out_ = 'yy';
leave looplabel;

end if ;
end loop;
end;
 
set @out2 = '';
call p1_(1,@out2);
select @out2 from dual;

#while do
drop procedure if exists p2_ ;
create procedure p2_(in in_2 int, in out_2  varchar(3))
begin 
declare v_ int ;
set v_ = 10 ;
while v_ > 5 do
set v_ = v_ - 1 ;
select in_2 , out_2 ,v_ from dual;
end while ;
end;

set @out_2 = 'KKrr';  
call p2_(10,@out_2);



#repeat
drop procedure  if exists p3 ;

create procedure p3 (in in_3 int , out out_3 varchar(100) )

begin 

declare v3 int  ;

set v3  = in_3 ;

repeat 

select v3 , in_3,  out_3  from dual;

set v3 = v3 - 1;

until v3 < 5 end repeat ;

end;

set @inp = 6 ,@outp = 'MM';

call p3(@inp ,@outp);

# set 和 declare 申明变量的区别 : set 全局, declare 局部 且只能在 begin之后出现
DROP PROCEDURE IF EXISTS temp;
CREATE PROCEDURE temp()
 
BEGIN
   
DECLARE a INT DEFAULT 1;
    SET a=a+1;
    SET @b=@b+1;
    SELECT a,@b;

END
set @b = 1; #@b字母不区分大小写
call temp();#执行一次,@b变量就增加1 ,@b相当于全局变量


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值