MySql存储过程
定义存储过程格式:
create procedure 存储过程名(参数)
begin
...
select 要返回的内容;
end;
1 定义一个简单的存储过程
create procedure test()
begin
select * from specialty;
end;
call test;
create procedure test1()
begin
-- declare定义变量
declare s_count int default 0 ;
declare s_result varchar(100);
-- select直接加内容表示返回,存储过程遇到第一个select直接加内容就直接返回了,不会执行后边的语句
#select s_count+10;
-- set为变量赋值
set s_count = 100;
-- 使用into将查询结果赋值给变量
select count(1) into s_count from specialty;
if s_count > 5 then
set s_result = '个数大于5';
else
set s_result = '个数不大于5';
end if;
select s_result;
end;
call test1();
-- 创建带有参数的存储过程,如果写了参数,则调用时必须 传递
-- 参数类型【in】:输入参数,默认类型。注意,in参数是只读的,就是说即使在存储过程中改变了in类型参数的值,存储过程结束后仍保留期原始值
create procedure test2(in flag tinyint)
begin
declare result varchar(100);
if flag=0 then
set result = '假';
else
set result = '真';
end if;
select result,flag;
end;
call test2(true);
-- 参数类型【out】:输出参数,可以作为返回值。注意,存储过程在启动时无法访问out参数的初始值
create procedure test3(in flag tinyint, out result varchar(20))
begin
-- 因为无法访问out类型的result初始值,所以这个条件永远不会满足
if flag=0 and result='a' then
set result = '假的';
else
set result = '真';
end if;
select result;
end;
set @a := 'a';
call test3(false,@a);
select @a;
-- 参数类型【inout】存储过程可以修改INOUT参数也可以获取其初始值并将新值传递回调用程序
create procedure test4(inout result varchar(10))
begin
if result = 'a' then
set result = 'aaa';
else
set result = 'bbb';
end if;
end;
set @a := 'b';
call test4(@a);
select @a;
-- 创建一个存储过程,根据月份判断季度
create procedure judgmentQuarter(in month int, out quarter varchar(10))
begin
case
when month >= 1 and month <=3 then
set quarter = '第一季度';
when month >= 4 and month <= 6 then
set quarter = '第二季度';
when month >= 7 and month <= 9 then
set quarter = '第三季度';
else
set quarter = '第四季度';
end case;
end;
call judgmentQuarter(12,@quarter);
select @quarter;
-- 删除存储过程/函数
drop procedure test3;
drop function 函数名
-- 查看存储过程/函数的创建语句
show create procedure test1;
show create function 函数名;
-- 查看shixun数据库的所有存储过程
select name from mysql.proc where db='shixun';
-- 查看存储过程/函数的状态信息
show procedure status;
show function status;
创建存储函数格式:
create function 函数名(参数)
returns 返回类型(长度)
begin
函数体;
end;
-- 创建一个存储函数,根据课程号返回课程名
create function findCourseName(courseID int)
returns char(20)
begin
declare courseName varchar(20);
select course_name into courseName from course where course_id=courseID;
return courseName;
end;
select findCourseName(0000);
存储过程和存储函数的区别:
1.存储函数有且只有一个返回值,而存储过程可以有多个返回值,也可以没有返回值
2.存储函数只能有输入参数,而且不能带in, 而存储过程可以有多个in,out,inout参数
3.存储过程中的语句功能更强大,存储过程可以实现很复杂的业务逻辑,而函数有很多限制,如不能在函数中使用insert,update,delete,create等语句
4.存储函数只完成查询的工作,可接受输入参数并返回一个结果,也就是函数实现的功能针对性比较强
5.存储过程可以调用存储函数、但函数不能调用存储过程
6.存储过程一般是作为一个独立的部分来执行(call调用)。而函数可以作为查询语句的一个部分来调用
分类:
数据库
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤