针对新手的MYSQL存储过程详解

一位爱好技术的橘右京的哥哥橘左京

在这里插入图片描述

前言:什么是存储过程?存储过程就像是一个接口,可以直接去调用,不需要重复的编写。

1.1 存储过程和函数概述

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

1.2 创建存储过程

语句:

delimiter $
create procedure 存储过程名称()
begin
SQL语句;
end$

释义:delimiter的中文解释为‘分隔符’,表示将“$”代替“;”设置为分隔符,因为在begin后的SQL语句中需要以";“结尾,所以就要设置一个与”;"区分开的分隔符。

1.3 调用存储过程

语法:call 存储过程名称;
执行创建存储过程完成后,使用调用方法调用存储过程。

1.4 查看存储过程

查询数据库中的存储过程

select name from mysql.proc where db = 'test'

查询存储过程的状态信息

show procedure status;

1.5 变量

· DECLARE
通过DECLARE可以定义一个变量,该变量的作用范围用于BEGIN…END之间

1.5.1 声明变量示例:
案例:声明一个年龄变量并查询值

delimiter $
create procedure 存储过程名称()
begin
declare age int default 0;
select  concat('num的值是',age);
end $

1.5.2 SET赋值示例:
案例:声明一个年龄变量并通过SET赋值后查询值

delimiter $
create procedure 存储过程名称()
begin
declare num int default 0;
set num = num + 10;
select  concat('num的值是',num);
end $

1.5.3 select …into…赋值示例:
案例:声明一个年龄变量并通过SELECT INTO 赋值后查询值

delimiter $
create procedure 存储过程名称()
begin
declare num int;
select count(1) into num from bsx_user;
select num;
end $

1.6 IF语法判断

案例:当年龄段为12岁以下输出青年,当年龄在12-17输出青少年,18-29岁输出青年

delimiter $
create procedure test01()
begin
declare age int default 15;
declare words varchar(20) default 10;
if age <=11 then
set words = '儿童';
elseif age >= 12 and age <= 17 then 
set words = '青少年';
elseif age >=18 and age <= 29 then
set words = '青年';
end if;
select concat('年龄:',age,'属于:',words);
end $

1.7 输入/输出 参数

1.7.1 案例:输入参数 传参
– 当年龄段为12岁以下输出儿童,当年龄在12-17输出青少年,18-29岁输出青年

语法:create procedure test01([in/out/inout] 参数名 参数类型)
IN: 默认为该方法 调用方传入值作为输入参数
OUT: 该参数作为输出,也就是参数可以作为返回值
OUTIN: 既可以作为输入参数,也可以作为输出参数

创建可传入参数的存储过程:

delimiter $
create procedure test01(in age int)
begin
declare age int default 15;
declare words varchar(20) default 10;
if age <=11 then
set words = '儿童';
elseif age >= 12 and age <= 17 then 
set words = '青少年';
elseif age >=18 and age <= 29 then
set words = '青年';
end if;
select concat('年龄:',age,'属于:',words);
end $

调用存储过程:

call test01(15)

1.7.2 案例:输出参数 传参
– 当年龄段为12岁以下返回儿童,当年龄在12-17返回青少年,18-29岁返回青年
根据传入的年龄大小返回对应的年龄区间名称:

delimiter $
create procedure test01(in age int,out words varchar(20))
begin
if age <=11 then
set words = '儿童';
elseif age >= 12 and age <= 17 then 
set words = '青少年';
elseif age >=18 and age <= 29 then
set words = '青年';
end if;
end $

调用存储过程:

call test01(15)

查询返回的结果:

select @words ;         (words是声明的变量名)

小知识:
@words:像这种前面带着@符号的变量称为会话变量,在整个会话过程起作用,类似于全局变量。
@@words:带有两个@@符号的被称为系统变量。

1.8 CASE语法结构

1.8.1 方式一(基本的CASE语法结构)
语法:

case XXX
when 1 then
赋值;
when 2 then
赋值;
else
赋值;
end case;

创建存储过程:
(输入1返回1岁 输入2返回2岁,输入其他文本输出"其他")

delimiter $
create procedure test01(age int,out words varchar(20))
begin 
case age
when 1 then
set words = '一岁';
when 2 then
set words = '二岁';
else  
set words = '其他';
end case;
end $;	

调用存储过程:

call test01(1,@words)

查询返回的结果:

select @words;

1.8.2 方式二(带有表达式的CASE语法结构)
语法:

case
when 表达式 then
赋值;
else
赋值;
end case;

创建存储过程:
(输入0-12 返回儿童,输入13-17返回青少年,输入18-29返回青年,输入其他值返回“老年”)

delimiter $
create procedure test01(age int)
begin 
declare words varchar(20);
case 
when age >=0 and age <= 12 then
set words = '儿童';
when age >=13 and age <= 17 then
set words = '青少年';
when age >=18 and age <= 29 then
set words = '青年';
else  
set words = '老年';
end case;
select words;
end $;

调用存储过程查询返回的结果:

call test01(15);

1.9 While循环

特征:满足条件继续循环
语法:

while a<=10 do

end while;

创建存储过程:
(输入一个数,累加超过这个数字时候停止循环并显示数字)

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

调用存储过程:

call test01(2)

1.10 repeat循环

特征:满足条件退出循环
语法:

repeat
循环语句…
until 条件语句
end repeat;

创建存储过程:

(计算 1+到n的值)
delimiter $
create procedure test01(n int)
BEGIN
declare num int default 0;
repeat 
set num = num + n;
set n = n-1;
until  n = 0
end repeat;
select num;
end $

调用存储过程:

call test01(5)

1.11 loop循环

特征:满足条件退出循环
语法:

XX:loop (XX代表别名)
循环语句…
if XX<=0 then (因为loop不带有停止循环的判断语句 所以用IF)
leave c;
end if;
end loop c;

创建存储过程:
(计算 1+到n的值)

delimiter $
create procedure test01(n int)
BEGIN
declare num int default 0;
c:loop
	set num = num + n;
	set n = n - 1;
	if n <= 0 then
	leave c;
	end if;
end loop c;
select num;
end $

调用存储过程:

call test01(5)

1.12 游标/光标

游标是用来存储查询结果集的数据类型,在存储过程和函数中可以使用光标对结果集进行循环的处理,光标的使用包括光标的声明,OPEN、FETH、和Close,其语句分别如下:
声明光标:
Declare 光标名称 CURSOR for 查询语句;

OPEN光标:
open 光标名称;
FETCH光标:
fetch 光标名 into 声明的字段名;
CLOSE光标:
close 光标名;

创建游标存储过程:

1.delimiter $
2.create procedure test01()
3.begin
4.declare u_id int(11); 
5.declare u_name varchar(20);
6.declare u_word varchar(20);
7.declare u_user cursor for select * from bsx_user;
8.open u_user;
9.fetch u_user into u_id,u_name,u_word;
10.close u_user;
11.select u_id,u_name,u_word;
12.end $

逐行说明:

1.声明隔断符
2.创建存储过程
3.开始
4.声明u_id
5.声明u_name
6.声明u_word
7.声明名为u_user的光标并执行查询语句
8.打开光标
9.将查询的结果写入声明的变量内
10.结束光标
11.查询声明的变量是否成功赋值
12.结束光标

调用存储过程:

使用循环方式创建存储过程:
delimiter $
create procedure test01()
BEGIN	
declare e_id int;
declare e_name varchar(20);
declare e_word varchar(20);
declare e_status int default 1;
declare e_user cursor for select * from bsx_user;
declare exit exits for NOT found set e_status = 0;
open e_user;
repeat 
fetch e_user into e_id,e_name,e_status ;
select e_id,e_name,d_word;
until e_status = 0;
end repeat;
close e_user;
end $

1.13 存储函数

语法结构:

create function test01([param type,param type])
return type
begin

end ;

创建存储函数:

delimiter $
create function test01(userName int)
returns int
begin
declare count int;
select count(1) into  count from bsx_user where user_name = userName;
return count;
end $

调用存储函数:

elect test01(1)

觉得有帮助可以收藏文章

在这里插入图片描述

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值