加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 创业 > C语言 > 正文

mysql--存储过程 + 函数

发布时间:2020-12-15 00:14:27 所属栏目:C语言 来源:网络整理
导读:I. 存储过程 创建存储过程 参数声明 变量声明 设置变量 while 循环语句 条件语句(if | case) call 调用存储过程 示例代码: delimiter // create procedure cal(in sNum int,in eNum int,out total int) comment '计算函数' begin declare tmpVal int defaul

I. 存储过程

  1. 创建存储过程

  2. 参数声明

  3. 变量声明

  4. 设置变量

  5. while 循环语句

  6. 条件语句(if | case)

  7. 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');

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读