七:流程控制
定义变量
1declare 变量名 类型 default 值; 2例如: declare i int default 0;if语句的使用
1# 语法 2if 条件 then 3语句; 4end if; 5第二种 if elseif 6if 条件 then 7语句1; 8elseif 条件 then 9语句2; 10else 语句3; 11end if; 12 13# 案例:编写过程 实现 输入一个整数type 范围 1 - 2 输出 type=1 or type=2 or type=other; 14create procedure showType(in type int,out result char(20)) 15begin 16if type = 1 then 17set result = "type = 1"; 18elseif type = 2 then 19set result = "type = 2"; 20else 21set result = "type = other"; 22end if; 23endCASE语句(选择语句)
大体意思与Swtich一样的 你给我一个值 我对它进行选择 然后执行匹配上的语句
1# 语法 2create procedure caseTest(in type int) 3begin 4CASE type 5when 1 then select "type = 1"; 6when 2 then select "type = 2"; 7else select "type = other"; 8end case; 9endLOOP循环
没有条件,需要自己定义结束语句
1# 语法 2create procedure showloop() 3begin 4declare i int default 0; 5aloop: LOOP 6select "hello loop"; 7set i = i + 1; 8if i > 9 then leave aloop; 9end if; 10end LOOP aloop; 11endREPEAT循环
1#类似do while 2#输出10次hello repeat 3create procedure showRepeat() 4begin 5declare i int default 0; 6repeat 7select "hello repeat"; 8set i = i + 1; 9until i > 9 10end repeat; 11end 12 13#输出0-100之间的奇数 14create procedure showjishu() 15begin 16declare i int default 0; 17aloop: loop 18set i = i + 1; 19if i >= 101 then leave aloop; end if; 20if i % 2 = 0 then iterate aloop; end if; 21select i; 22end loop aloop; 23end