mysql-存储过程

7 篇文章 0 订阅

mysql存储过程

存储过程就是把Sql语句经编译存储到数据库里面变成持久化

我们平常的sql语句是无法保存到数据库里面的
但是存储过程可以保存到数据库里面
存储过程可以简化应用开发人员的很多工作
减少数据库和服务器传输 提高数据处理效率
存储函数;是一个有返回值的过程
存储过程:是一个没有返回值的方法

创建存储过程

创建一个查询student表查询所有数据的存储过程

语法:
create procedre 存储过程名称 ([proc_parameter[...]])
begin
    -- SQL语句
end
案例:
create procedure test0111()
begin
select * from student;
end
$

然后调用这个存储过程

call test0111();

删除存储过程

drop procedure 存储过程名称;

查看存储过程

select name from mysql.proc where db = '数据库名称';

查看存储过程状态

show procedure status;

查看存储过程定义

show create procedure 数据库名称.存储过程名称 \G;

存储过程 变量

定义变量
在存储过程中 , declare 关键字定义局部变量,其作用域只能在begin…end中

语法:
create procedure  存储过程名称()
begin
    declare 变量名 变量类型 [default 值];
end
案例:
 create procedure test()
 begin
     declare id  int;    
     SET id =  0;
 end
select ... into ....:
create procedure 存储过程名称()
begin 
    declare 变量名 变量类型;
    select 查询字段 into 变量名 from 表名;
end

存储过程 条件判断 / 传参

条件判断 if else

语法:
create procedure 存储过程名称()
begin
    if 条件表达式 then 逻辑1;
    elseif 条件表达式 then 逻辑2;
    else 逻辑3;
    end if;
end 
案例:
create procedure test()
begin
	declare a int default 175;
    declare b varchar(30);
    if  a>=1 then
    set b="哈哈哈";
    elseif a>=10 then a<=2;
    else 
    set a="哈哈哈111";
    end if;
end 

传递参数
in : 该参数可以作为输入参数,也就是需要调用方传入的值 默认项
out : 该参数可以作为输出参数,也就是可以作为返回值给调用方
inout: 该参数可以作为输入,也可以作为输出

语法:
create procedure 存储过程名称(int 参数名,out 参数名 参数类型[(参数长度)],inout 参数名 参数类型)
begin
    ...
end
create procedure test06(in height int)
begin
    
    declare desci varchar(30);

    if height >= 180 then
        set desci = "大";
    elseif height >= 170 and height < 180 then
        set desci = "中";
   else 
       set desci = "小";
   end if;

    select desci;

end
例:
create procedure test07(in height int ,out desci varchar(30))

begin
    if height >= 180 then set desci = "大";
    elseif height < 180 and height >= 170 then set desci = "中";
    else set desci = "小";
    end if;
end

调用

call(168,@desci);

输出 out

select @desci;

条件判断case

	语法
	create procedure 存储过程名称([in 参数名称 参数类型,out 参数名称 参数类型])
begin
    case
        when 条件1 then 逻辑1;
        when 条件2 then 逻辑2;
        when 条件3 then 逻辑3;
    end case;
end
演示:
创建存储过程

create procedure test10(in month int,out result varchar(30))
begin
case 
    when month >= 1 and month <= 3 then set result = "第一季度";
    when month >= 4 and month <= 6 then set result = "第二季度";
    when month >= 7 and month <= 9 then set result = "第三季度";
    when month >= 10 and month <= 12 then set result = "第四季度";
end case; 
    set result = concat("你输入的是第",month,"月份,它属于",result);
end 

调用
    call test10(4,@result);
输出
    select @result;

while循环

语法:
while  条件表达式  do
    ....
end while;
实例:
create procedure test11(in n int)
begin
    declare total int default 0;
    declare num int default 0;
    while num <= n do
        set total = total + num;
        set num = num + 1;
    end while;
    select total;
end

repeat循环

语法:
repeat
    ....
    until 条件表达式
end repeat
实例
create procedure test12(in n int)
begin
    declare total int default 0;
    repeat
        set total = total + n;
        set n = n - 1;
        until n = 0
    end repeat;
    select total;
end

loop循环

语法:
标识符:loop
    
    if 条件表达式 then
        leave 标识符;
    end if;
    
    ....
    
end loop 标识符;
实例
create procedure test13(in n int)
begin
    declare total int default 0;
    ins:loop
        if n <= 0 then
            leave ins;
        end if;
        
        set total = total + n;
       set n = n - 1;
   end loop ins; 
   
   select total;
end
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值