I. 存储过程
创建存储过程
参数声明
变量声明
设置变量
while 循环语句
条件语句(if | case)
call 调用存储过程
示例代码:
delimiter //
create procedure cal(in sNum int,in eNum int,out total int) comment '计算函数' begin
declare tmpVal int default 0;
set tmpVal = 0;
while sNum <= eNum do
tmpVal = tmpVal + sNum;
sNum = sNum + 1;
end while;
-- 方式1: case when condition then value else value end;
-- set rel = case when tmpVal < 50 then set total = 0
-- when tmpVal > 50 then set total = tmpVal
-- else
-- set tmpVal = 50
-- end;
-- 方式2:if condition then statement elseif condition statement else statement end if
if total < 50 then
set rel = 0;
elseif total > 50 then
set rel = total;
else
set rel = 50;
end if;
end//
delimiter ;
call cal(1,10,@total) // @total = 55
select @total; // 55
II. 函数
delimiter //
create function sayName(name char(50)) returns varchar(255) deterministic
begin
return concat('欢迎 ',name,'!');
end//
delimiter ;
select sayName('cxl');
原文链接:https://www.f2er.com/note/421005.html