MySQL存储过程是一种在数据库中预编译并存储的SQL语句集合,可以用来执行特定的任务或复杂的操作。通过使用存储过程,开发者能够提高SQL代码的重用性、执行效率以及安全性。存储过程允许将复杂的业务逻辑封装在数据库中,从而减少应用程序代码中的复杂度,同时降低了数据库操作的网络延迟。
在本篇文章中,我们将深入探讨MySQL存储过程的基本概念、创建和使用方法,帮助你掌握如何在实际项目中高效地利用存储过程来简化数据库操作。
1.修改结束符
delimiter /
2.存储过程实例
(1)创建存储过程格式:
create procedure 存储过程名称(in or out 参数1 数据类型,in or out 参数2 数据类型.........)
begin
xxxxx;
end;
结束符
其中:in指的是参数为输入参数 把存储过程外面的值 输入到存储过程中 out指的是将存储过程中计算出来的值 输出到外部 in和out 如果不写默认为in
(2)实例
create procedure var_out(in a varchar(80),out res varchar(80))
begin
set res='你好啊!';
select a;
end;
/
set @o1='0'; --定义变量 用来接收存储过程的输出参数
call var_out('节昊文',@o1); --调用存储过程 执行存储过程的内容
--节昊文被赋值给输入参数a 并通过select a;语句返回
--@o1是传递给输出参数res的地址 存储过程内部会将'你好啊!'赋值给@o1 通过set res='你好啊!'
select @o1; --查询并显示@o1的值
/
3.在存储过程中使用if else
create procedure ifelse(a int,b int,out res int)
begin
if a>b then
set res=a+b;
else
set res =a-b;
end if;
end;
/
set @o1=0;
call ifelse(1,2,@o1);
select @o1;
/
set @o1=0; call ifelse(3,2,@o1); select @o1;/
4.在存储过程中使用loop循环打印12345
create procedure lp()
begin
declare i int default 0;
lp:loop
set i=i+1;
select i;
if i=5 then
leave lp;
end if;
end loop;
end;
/
call lp();
/
5.在存储过程中使用case when (常用)
create procedure case_when(a int)
begin
case a
when 1 then
select 'case 1';
when 2 then
select 'case 2';
else
select 'others';
end case;
end;
/
call case_when(1);
/
call case_when(2);
/
call case_when(3);
/
6.使用存储过程存储MySQL表中记录(一条)
存储MySQL表中的多个值 可以声明多个变量
create procedure db_to_var(id int)
begin
declare name varchar(30) default '';
declare dept_id int default 0;
select employee_name,department_id into name,dept_id from employees where employee_id=id;
select name,dept_id;
end;
/
call db_to_var(1);
/
call db_to_var(5);
/
7.如何使用存储过程提取MySQL表中所有数据(游标:cusor类型)
游标是MySQL提供的一个数据类型 游标是一个指针 可以向下移动 游标移动时 它指向谁 我们就可以取谁的值 内存地址 指向的是select语句的查询结果 在存储过程当中 我们可以利用游标取表中的每一条记录
如果想取出每一条记录 需要定义一个游标 使用loop循环取每一条记录 让游标每次向下移动 并监视游标 如果游标走到最后一条记录 退出循环
declare continue handler for not found set isExit=TURE; --监视游标 如果游标走到最后一条记录 isExit会自动变为Ture
create procedure fetch_all()
begin
declare id int default 0;
declare name varchar(80) default '';
declare isExit int default FALSE;
declare cs cursor for select employee_id,employee_name from employees;
declare continue handler for not found set isExit=TRUE;
open cs;
lp:loop
fetch cs into id,name;
select id,name;
if isExit then
leave lp;
end if;
end loop;
close cs;
end;
/
call fetch_all();
/
可以看到 最后一条信息被打印了两次 当游标查询最后一行时 会出现重复查询的问题 这是因为在循环结束前会执行一次fetch cs into id,name; 然后再检查isExit变量 导致最后一次查询重复 为了解决这个问题 可以在fetch cs into id,name;前加入判断条件 避免重复查询 下面是修改后的存储过程代码:
create procedure fetch_all()
begin
declare id int default 0;
declare name varchar(80) default '';
declare isExit int default FALSE;
declare cs cursor for select employee_id,employee_name from employees;
declare continue handler for not found set isExit=TRUE;
open cs;
lp:loop
fetch cs into id,name;
if isExit then
leave lp;
end if;
select id,name;
end loop;
close cs;
end;
/
call fetch_all();
/
8.删除存储过程
drop procedure fetch_all();
/
9.恢复结束符
delimiter ;
通过本文的学习,你已经了解了MySQL存储过程的基础知识以及如何创建和调用它们。存储过程不仅能够提高数据库操作的效率,还能增强数据处理的灵活性和安全性。掌握存储过程的使用,将大大提升你在数据库管理和开发中的能力。
希望你能在实际开发中应用存储过程,以优化你的数据库设计。如果有任何疑问,欢迎查阅MySQL的官方文档或进一步与社区交流。祝你在数据库编程的道路上越走越远!