mysql存储过程和函数

一、存储过程

1 .存储过程和函数概述

存储过程和函数是 事先经过编译并存储在数据库中的一段 SQL 语句的集合,调用存储过程和函数可以简化应用开
发人员的很多工作,减少数据在数据库和应用服务器之间的传输,对于提高数据处理的效率是有好处的。
存储过程和函数的区别在于函数必须有返回值,而存储过程没有。
函数 : 是一个有返回值的过程 ;
过程 : 是一个没有返回值的函数 ;

2.创建存储过程

CREATE PROCEDURE procedure_name ([proc_parameter[,...]]) 
begin
-- SQL语句 
end ;

eg:

delimiter $    --声明分隔符为$
create procedure pro_test1() 
begin
select 'Hello Mysql' ; 
end$ 
delimiter ;

在这里插入图片描述

3 调用存储过程

call procedure_name() ;

在这里插入图片描述

4 查看存储过程

-- 查询存储过程的状态信息
 show procedure status; 
 -- 查询某个存储过程的定义,demo_01是数据库名
  show create procedure demo_01.pro_test1 \G;

在这里插入图片描述

5 删除存储过程

DROP PROCEDURE [IF EXISTS] sp_name;

6 语法

存储过程是可以编程的,意味着可以使用变量,表达式,控制结构 , 来完成比较复杂的功能

6.1 变量

DECLARE,通过 DECLARE 可以定义一个局部变量,该变量的作用范围只能在 BEGIN…END 块中。
DECLARE var_name[,...] type [DEFAULT value]
eg:

delimiter $ 
create procedure pro_test2() 
begin
declare num int default 5; 
select num+ 10;
end$ 
delimiter ;

在这里插入图片描述

SET
直接赋值使用 SET,可以赋常量或者赋表达式

DELIMITER $ 
CREATE PROCEDURE pro_test3() 
BEGIN 
 DECLARE NAME VARCHAR(20); 
 SET NAME = 'MYSQL'; 
 SELECT NAME ; 
 END$ 
 DELIMITER ;

在这里插入图片描述

6.2 if条件判断

语法结构 :

if search_condition then statement_list
	[elseif search_condition then statement_list] ...
	[else statement_list]
end if;
delimiter $
create procedure pro_test6()
begin
declare height int default 175;
declare description varchar(50);
if height >= 180 then
set description = '身材高挑';
elseif height >= 170 and height < 180 then
set description = '标准身材';
else
set description = '一般身材';
end if;
select description ;
end$
delimiter ;

在这里插入图片描述

6.3 传递参数

存储过程可以使用语法,判断,传参
create procedure procedure_name([in/out/inout] 参数名 参数类型) ...
IN : 该参数可以作为输入,也就是需要调用方传入值 , 默认
OUT: 该参数作为输出,也就是该参数可以作为返回值 INOUT: 既可以作为输入参数,也可以作为输出参数
eg:

  • IN - 输入
delimiter $
create procedure pro_test5(in height int)
begin
declare description varchar(50) default '';
if height >= 180 then
set description='身材高挑';
elseif height >= 170 and height < 180 then
set description='标准身材';
else
set description='一般身材';
end if;
select concat('身高 ', height , '对应的身材类型为:',description);
end$
delimiter ;

在这里插入图片描述

  • OUT-输出
delimiter $
create procedure pro_test5(in height int , out description varchar(100))
begin
if height >= 180 then
set description='身材高挑';
elseif height >= 170 and height < 180 then
set description='标准身材';
else
set description='一般身材';
end if;
end$

调用:

call pro_test5(168, @description)$ 
select @description $

在这里插入图片描述

小知识
@description : 这种变量要在变量名称前面加上“@”符号,叫做用户会话变量,代表整个会话过程他都是有作用
的,这个类似于全局变量一样。 @@global.sort_buffer_size : 这种在变量前加上 “@@” 符号, 叫做 系统变量

6.4 case结构

语法结构 :

方式一 :
CASE case_value
	WHEN when_value THEN statement_list
	[WHEN when_value THEN statement_list] ...
	[ELSE statement_list]
END CASE;
方式二 :
CASE
	WHEN search_condition THEN statement_list
	[WHEN search_condition THEN statement_list] ...
	[ELSE statement_list]
END CASE;

给定一个月份, 然后计算出所在的季度
eg:

delimiter $
create procedure pro_test9(month int)
begin
declare result varchar(20);
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;
select concat('您输入的月份为 :', month , ' , 该月份为 : ' , result) as content ;
end$
delimiter ;

CALL pro_test9(4);

在这里插入图片描述

6.5 while循环

语法结构:

while search_condition do
	statement_list
end while;

eg:
计算从1加到n的值

delimiter $
create procedure pro_test8(n int)
begin
declare total int default 0;
declare num int default 1;
while num<=n do
set total = total + num;
set num = num + 1;
end while;
select total;
end$
delimiter ;

-- 1到5累加
CALL pro_test8(5);

在这里插入图片描述

6.6. repeat结构

有条件的循环控制语句, 当满足条件的时候退出循环 。while 是满足条件才执行,repeat 是满足条件就退出循环。
语法结构:

REPEAT
	statement_list
	UNTIL search_condition
END REPEAT;

计算从1加到n的值

delimiter $
create procedure pro_test10(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$
delimiter ;

在这里插入图片描述

6.7 loop语句与leave语句

LOOP 实现简单的循环,退出循环的条件需要使用其他的语句定义,通常可以使用 LEAVE 语句实现,具体语法如下:

[begin_label:] LOOP
	statement_list
END LOOP [end_label]

如果不在 statement_list 中增加退出循环的语句,那么 LOOP 语句可以用来实现简单的死循环。
用来从标注的流程构造中退出,通常和 BEGIN … END 或者循环一起使用。下面是一个使用 LOOP 和 LEAVE 的简单例子 , 退出循环:

--  计算从1到n的   loop ...Leave

delimiter $
CREATE PROCEDURE pro_test11 ( n INT ) 
BEGIN
	DECLARE
		total INT DEFAULT 0;
	
	-- loop循环的别名 C
	c :LOOP
		IF
			n <= 0 THEN
				LEAVE c;
			
		END IF;
		
		SET total = total + n;
		
		SET n = n - 1;
		
	END LOOP c;
	SELECT	total;

END $delimiter;


CALL pro_test11(100);

在这里插入图片描述

6.8 游标/光标

游标是用来存储查询结果集的数据类型 , 在存储过程和函数中可以使用光标对结果集进行循环的处理。光标的使用
包括光标的声明、OPEN、FETCH 和 CLOSE,其语法分别如下。

--声明光标:
DECLARE cursor_name CURSOR FOR select_statement ;
--OPEN 光标:
OPEN cursor_name ;
--FETCH 光标:
FETCH cursor_name INTO var_name [, var_name] ...
--CLOSE 光标:
CLOSE cursor_name ;

eg:

-- 查询emp表中数据, 并逐行获取进行展示
 create procedure pro_test11()
begin
 declare e_id int(11); 
declare e_name varchar(50); 
declare e_age int(11);
declare e_salary int(11); 
declare emp_result cursor for select * from emp; open emp_result; 
fetch emp_result into e_id,e_name,e_age,e_salary; select concat('id=',e_id , ', name=',e_name, ', age=', e_age, ', 薪资为: ',e_salary); 
fetch emp_result into e_id,e_name,e_age,e_salary; select concat('id=',e_id , ', name=',e_name, ', age=', e_age, ', 薪资为: ',e_salary); 
fetch emp_result into e_id,e_name,e_age,e_salary; select concat('id=',e_id , ', name=',e_name, ', age=', e_age, ', 薪资为: ',e_salary);
fetch emp_result into e_id,e_name,e_age,e_salary; select concat('id=',e_id , ', name=',e_name, ', age=', e_age, ', 薪资为: ',e_salary); 
fetch emp_result into e_id,e_name,e_age,e_salary; select concat('id=',e_id , ', name=',e_name, ', age=', e_age, ', 薪资为: ',e_salary); 
close emp_result;
 end $ 

调用:
在这里插入图片描述
eg:
用循环的方法遍历游标

DELIMITER $
create procedure pro_test12()
begin
	DECLARE id int(11);
	DECLARE username varchar(50);
	DECLARE age int(11);
	DECLARE salary int(11);
	DECLARE has_data int default 1;
	-- 创建游标,将select * from emp查询结果放在游标里
	DECLARE emp_result CURSOR FOR select * from emp;
	DECLARE EXIT HANDLER FOR NOT FOUND set has_data = 0;
	-- 打开游标
	open emp_result;
	repeat
	  -- 使用游标(从游标中获取数据)
		fetch emp_result into id , username , age , salary;
		select concat('id为',id, ', name 为' ,username , ', age为 ' ,age , ', 薪水为: ',salary);
	until has_data = 0
	end repeat;
	
	-- 关闭游标
	close emp_result;
end$
DELIMITER ;

CALL pro_test12();

在这里插入图片描述

二、存储函数

存储过程和函数的区别在于函数必须有返回值,而存储过程没有。
函数 : 是一个有返回值的过程 ;
过程 : 是一个没有返回值的函数 ;
语法结构:

CREATE FUNCTION function_name([param type ... ]) RETURNS type
 BEGIN
 ...
  END;

eg:
定义一个存储过程, 请求满足条件的总记录数 ;

-- set global log_bin_trust_function_creators=TRUE;

delimiter $
CREATE FUNCTION count_city ( countryId INT ) RETURNS INT BEGIN
	DECLARE
		cnum INT;
	SELECT
		count(*) INTO cnum 
	FROM
		city 
	WHERE
		country_id = countryId;
	RETURN cnum;

END $delimiter;

select count_city(1);

调用:

select count_city(1); 

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值