备注 | 修改日期 | 修改人 |
CREAT | 2021-04-07 14:32:37[当前版本] | 系统管理员 |
变量 存储过程或函数: 声明变量: declare 变量名 数据类型 赋值: select 字段 into 变量名 输出变量:select 变量名 函数或存储过程外声明变量: set @变量名 = 初始值;
语法: if 条件 then 代码块; elseif 条件 then 代码块; end if; -- * 在里的条件不能使用()包含起来;*************** case -- case when then 的第一种用法 when 条件 then 代码; when 条件 then 代码; else 代码; end case; case num -- case when then的第二种使用方法,相当于开发中编程的switch when 1 then 代码块; when 2 then 代码块; else 代码块; end case; 例子: -- if 条件语句 -- 创建存储过程实现 if elseif end if drop procedure if exists proc_ifelseif; create procedure proc_ifelseif(in num int) begin if num > 3 then select num as 'num大于3'; elseif num < 3 then select num as 'num小于3'; end if; end; call proc_ifelseif(2) -- case when then 条件语句第一种使用方法 -- 创建存储过程实现 case when then create procedure proc_casewhenthen(in num int) begin case when num > 3 then select num 'num大于3'; when num < 3 then select num 'num小于3'; else select '输出的代码块' end case; end; call proc_casewhenthen(1) -- case when then 条件语句第二种使用方法 create procedure proc_casewhenthen(in num int) begin case num when 1 then select num 'num大于3'; when 5 then select num 'num小于3'; else select '输出的代码块' end case; end; call proc_casewhenthen(2)
语法: -- * 一般看法中终止循环使用break,但是在理不能使用,需要使用leave 循环名才能终止循环,每一个loop中必须有一个leave loop。。。;不然循环不会结束 循环名:loop 代码块; end loop; 例子: -- loop循环 drop procedure if exists proc_Name; create procedure proc_Name() begin declare num int default 1; declare sum int default 0; loopsum:loop set sum = sum + num; set num = num + 1; if num > 100 then leave loopsum;-- 大于100 时结束循环 end if; end loop; select sum; end; call proc_Name()
语法: repeat 代码块 until 条件 -- 如果条件为true,结束循环 在里不能加分号 end repeat; 例子: -- repeat 循环 -- 创建存储过程 实现repeat drop procedure if exists proc_Name; create procedure proc_Name(in num int) begin declare sum int default 0; declare i int default 0; repeat set sum = sum + i; set i = i + 1; until num < i end repeat; select sum as '综合是'; end; call proc_Name(5);
语法: -- 条件为true 循环继续,否则结束循环 while 条件 do 代码块 end while; 例子: -- while循环 create procedure proc_xiaogeName() begin declare i int default 0; declare sum int default 0; while i < 100 do set sum = sum + i; set i = i + 1; end while; select sum as 'sum'; end; call proc_xiaogeName()