mysql存储过程快速了解

整篇文章为个人学习心得,之后会持续发表mysql高级进阶,javaweb框架等知识点文章,制作不易,加个关注
存储过程
优点:
提高开发效率,提高应用程序的性能,简化程序开发人员的操作,减少应用程序和是数据库服务器之间的流量。
缺点:
难以调试,开发和维护难度高,增加数据库服务器开销。
创建存储过程及其一些相关语法:

-- 创建一个存储过程,名为mypro;传入参数(参数)为a,传出参数(返回值)为b
create procedure mypro(in a int,out b int)
begin
	select '这是一个存储过程';
	set b=1;
end;
-- 调用存储过程
call mypro(1,@x);
-- 查看返回结果
select @x;
-- 显示名为demo的数据库中的存储过程
show procedure status where db='demo';
-- 显示以my开头的存储过程
show procedure status where name like '%my%';
-- 显示名为mypro的存储过程的源代码
show create procedure mypro;
-- 删除名为mypro的存储过程
drop procedure mypro;

存储过程中使用的一些语法:

  • 声明变量:
-- 声明一个int类型的变量,名为one
declare one int;
-- 声明两个varchar类型的变量,名字为a和b
declare a,b varchar(20);
  • If条件语句:
if a=0 then 
	select '等于0';
elseif a=1 then 
	select '等于1';
else 
	select '其他';
end if'
  • case条件语句(2种):
-- 指向范围区间,a为变量
case
	when a<0 then
		select '负数';
	when a>0 then
		select '正数';
	else select '其它';
end case;
-- a为变量
case a
	when 1 then
		select '1';
	when 2 then
		select '2';
	else select '其它';
end case;
  • while循环语句
-- a表示变量,满足a<5条件会执行do后的循环体,直到不满足则退出循环
while a<5 do
	select '循环了一遍';
	set a=a+1;
end while;
  • repeat循环:
declare a int default 0;-- 声明一个变量
repeat -- 循环开始
	set a=a+1;-- 循环体
until a>=5 -- 满足该条件将退出循环
end repeat;
  • loop循环
declare a int default 0;-- 声明一个变量
looptest:loop -- loop为循环关键字,looptest自定义的名字
	set a=a+1;-- 循环体
	if a>=5 then leave looptest;-- 当满足条件退出名为looptest的循环
	end if;
end loop;
发布了3 篇原创文章 · 获赞 6 · 访问量 1890

猜你喜欢

转载自blog.csdn.net/weixin_46364022/article/details/104434301