在mysql-workbench的存储过程中使用循环while,repeat,loop

while循环使用

CREATE DEFINER=`root`@`localhost` PROCEDURE `while_use`()
BEGIN
declare sum int default 0;
declare t int default 5;
while t>0 do
set sum=sum+1;
set t=t-1;
end while;
SELECT sum;
END



repeat用法:

CREATE DEFINER=`root`@`localhost` PROCEDURE `repeat_use`()
begin
declare a int default 10;
repeat
set a=a-1;
until a<5
end repeat;
select a;
end


loop用法

CREATE DEFINER=`root`@`localhost` PROCEDURE `test_loop`()
begin
declare t int default 0;
label:loop
set t=t+1;
if t>10 then leave label;
end if;
end loop label;
SELECT t;
end

你可能感兴趣的:(MySQL数据库)