mysql手册05_存储过程和存储函数
mysql手册05_存储过程和存储函数
存储过程和存储函数是存储在数据库中的一段SQL语句集合。
通过调用存储过程或存储函数,可以简化开发,减少数据在数据库和服务器间的传输。
存储过程和存储函数的区别是:存储函数有返回值。
基本语句:
注:在创建存储过程时,为了避免冲突,需要临时修改语句结束符号:
delimiter $创建存储过程:
delimiter $create procedure pro_test1()
begin
select 'Hello MySQL';
end$delimiter ;调用存储过程:
call pro_test1();+-------------+
| Hello MySQL |
+-------------+
| Hello MySQL |
+-------------+查看存储过程的两种方法:
select name from mysql.proc where db = "demo01"$
show procedure status\G;查看存储过程的定义/封装语句:
show procedure pro_test1\G;删除存储过程:
drop procedure pro_test1;
变量:
declare定义变量和set设置变量值:create procedure pro_test1()
begin
declare num int default 10;
set num = num+10;
select concat('num的值为:',num);
end$调用存储过程:
call pro_test1();
+-------------------------------+
| concat('num的值为:',num) |
+-------------------------------+
| num的值为:20 |
+-------------------------------+select..into..将查询结果赋值给变量:create procedure pro_test2()
begin
declare num int;
select count(*) into num from city;
select concat('city表中的记录数为:',num);
end$call pro_test2();
+--------------------------------------------+
| concat('city表中的记录数为:',num) |
+--------------------------------------------+
| city表中的记录数为:4 |
+--------------------------------------------+
if条件判断:
create procedure pro_test3()
begin
declare height int default 175;
declare description varchar(50) default '';
if height >=180 thenset description='高个子';
elseif height >=170 and height<180 thenset description='中等个子';
elseset description='小个子';
end if;
select concat('身高:', height,'对应的类型为:',description);
end$call pro_test3();
+-----------------------------------------------------------------+
| concat('身高:', height,'对应的类型为:',description) |
+-----------------------------------------------------------------+
| 身高:175对应的类型为:中等个子 |
+-----------------------------------------------------------------+
输入参数:
根据传递的身高变量,判定身高类型
create procedure pro_test4(in height int)
begin
declare description varchar(50) default '';
if height >=180 thenset description='高个子';
elseif height >=170 and height<180 thenset description='中等个子';
elseset description='小个子';
end if;
select concat('身高:', height,'对应的类型为:',description);
end$call pro_test4(198);
+-----------------------------------------------------------------+
| concat('身高:', height,'对应的类型为:',description) |
+-----------------------------------------------------------------+
| 身高:198对应的类型为:高个子 |
+-----------------------------------------------------------------+call pro_test4(178);
+-----------------------------------------------------------------+
| concat('身高:', height,'对应的类型为:',description) |
+-----------------------------------------------------------------+
| 身高:178对应的类型为:中等个子 |
+-----------------------------------------------------------------+call pro_test4(155);
+-----------------------------------------------------------------+
| concat('身高:', height,'对应的类型为:',description) |
+-----------------------------------------------------------------+
| 身高:155对应的类型为:小个子 |
+-----------------------------------------------------------------+
输出参数:
根据传入的身高变量,获取身高类型并返回
create procedure pro_test5(in height int, out description varchar(10))
begin
if height >=180 thenset description='高个子';
elseif height >=170 and height<180 thenset description='中等个子';
elseset description='小个子';
end if;
end$call pro_test5(188,@description);
select @description;
+--------------+
| @description |
+--------------+
| 高个子 |
+--------------+
注:@a表示用户会话变量,连接关闭时将释放。@@a表示系统变量。
case结构:
给定月份,计算所在季度
create procedure pro_test6(mon int)
begin
declare result varchar(10);
case
when mon>=1 and mon<=3 then
set result='第一季度';
when mon>=4 and mon<=6 then
set result='第二季度';
when mon>=7 and mon<=9 then
set result='第三季度';
else
set result='第四季度';
end case;
select concat('传递的月份为:', mon ,',计算出的结果为:', result) as content;
end$call pro_test6(9);
+---------------------------------------------------------------+
| content |
+---------------------------------------------------------------+
| 传递的月份为:9,计算出的结果为:第三季度 |
+---------------------------------------------------------------+
while循环:
计算从1加到n
create procedure pro_test7(n int)
begindeclare total int default 0;declare num int default 1;while num <= n doset total = total + num;set num = num +1;end while; select total;
end$call pro_test7(10);
+-------+
| total |
+-------+
| 55 |
+-------+
repeat循环:
计算从1加到n
注意until语句后面没有分号!
create procedure pro_test8(n int)
begindeclare total int default 0;repeatset total = total + n;set n = n-1;until n=0end repeat;select total;
end$call pro_test7(10);
+-------+
| total |
+-------+
| 55 |
+-------+
loop循环,leave退出:
计算从1加到n
create procedure pro_test9(n int)
begindeclare total int default 0;c:loopset total=total+n;set n = n-1;if n<=0 thenleave c;end if; end loop c;select total;
end$call pro_test9(10);
+-------+
| total |
+-------+
| 55 |
+-------+
游标:用来存储查询结果集:
一次fetch读取一行数据
若fetch次数超过数据行数,将报错
准备工作:
create table emp(id int(11) not null auto_increment,name varchar(50) not null comment '姓名',age int(11) comment '年龄',salary int(11) comment '薪水',primary key(id)
)engine=innodb default charset=utf8;insert into emp(id,name,age,salary) values(null,'金毛狮王',55,3800),(null,'白眉鹰王',60,4000),(null,'青翼蝠王',38,2800),(null,'紫衫龙王',42,1800);查询emp表的数据并逐行获取进行展示:
create procedure pro_test10()
begindeclare 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,', salary=',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,', salary=',e_salary);close emp_result;
end$call pro_test10();
+-------------------------------------------------------------------------+
| concat('id=',e_id,', name=',e_name,', age=',e_age,', salary=',e_salary) |
+-------------------------------------------------------------------------+
| id=1, name=金毛狮王, age=55, salary=3800 |
+-------------------------------------------------------------------------+
1 row in set (0.05 sec)+-------------------------------------------------------------------------+
| concat('id=',e_id,', name=',e_name,', age=',e_age,', salary=',e_salary) |
+-------------------------------------------------------------------------+
| id=2, name=白眉鹰王, age=60, salary=4000 |
+-------------------------------------------------------------------------+
1 row in set (0.05 sec)
循环获取游标数据:
注:退出条件的声明必须紧跟在游标声明之后!
查询emp表的数据并逐行获取进行展示:
create procedure pro_test11()
begindeclare e_id int(11);declare e_name varchar(50);declare e_age int(11);declare e_salary int(11);declare has_data int default 1;declare emp_result cursor for select * from emp;declare exit handler for not found set has_data=0;open emp_result;repeatfetch emp_result into e_id,e_name,e_age,e_salary;select concat('id=',e_id,', name=',e_name,', age=',e_age,', salary=',e_salary);until has_data=0end repeat;close emp_result;
end$call pro_test11();
+-------------------------------------------------------------------------+
| concat('id=',e_id,', name=',e_name,', age=',e_age,', salary=',e_salary) |
+-------------------------------------------------------------------------+
| id=1, name=金毛狮王, age=55, salary=3800 |
+-------------------------------------------------------------------------+
1 row in set (0.00 sec)+-------------------------------------------------------------------------+
| concat('id=',e_id,', name=',e_name,', age=',e_age,', salary=',e_salary) |
+-------------------------------------------------------------------------+
| id=2, name=白眉鹰王, age=60, salary=4000 |
+-------------------------------------------------------------------------+
1 row in set (0.00 sec)+-------------------------------------------------------------------------+
| concat('id=',e_id,', name=',e_name,', age=',e_age,', salary=',e_salary) |
+-------------------------------------------------------------------------+
| id=3, name=青翼蝠王, age=38, salary=2800 |
+-------------------------------------------------------------------------+
1 row in set (0.00 sec)+-------------------------------------------------------------------------+
| concat('id=',e_id,', name=',e_name,', age=',e_age,', salary=',e_salary) |
+-------------------------------------------------------------------------+
| id=4, name=紫衫龙王, age=42, salary=1800 |
+-------------------------------------------------------------------------+
1 row in set (0.01 sec)
存储函数:
定义一个存储函数,获取满足条件的总记录数:
create function fun1(countryId int)
returns int
begindeclare cnum int;select count(*) into cnum from city where country_id = countryId;return cnum;
end$调用存储函数:
select fun1(1);
+------+
| $ |
+------+
| 3 |
+------+删除存储函数:
drop function fun1;注:若创建存储函数报错:
This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)
使用以下命令解决:
set global log_bin_trust_function_creators=1$
mysql手册05_存储过程和存储函数相关推荐
- MySQL初级篇——存储过程、存储函数的相关概念及应用举例
文章目录: 1.什么是存储过程? 2.存储过程操作相关SQL 3.存储过程实操SQL 4.存储函数操作相关SQL 5.存储函数实操SQL 6.存储过程.存储函数的优缺点 1.什么是存储过程? 含义:存 ...
- 【MySQL进阶】存储过程及存储函数
5.存储过程 5.1.介绍 存储过程是事先经过编译并存储在数据库中的一段 SQL 语句的集合,调用存储过程可以简化应用开发人员的很多工作,减少数据在数据库和应用服务器之间的传输,对于提高数据处理的效率 ...
- MySQL入门之存储过程与存储函数
---------------------------------------------------------------------------------------------------- ...
- MYSQL中如何创建存储过程和存储函数(上篇)
存储程序分为存储过程和存储函数.在MySQL中创建存储过程和存储函数的语句分别是create procedure 和create function.使用call语句来调用存储过程,只能用输出变量返回值 ...
- MySQL存储过程与存储函数
1.创建存储过程 存储过程就是一条或者多条 SQL 语句的集合,可以视为批文件.它可以定义批量插入的语句,也可以定义一个接收不同条件的 SQL. 创建存储过程的语句为 "create pro ...
- 15【存储过程和存储函数】
上一篇:14[视图] 下一篇:16[数据库的范式] 目录:[MySQL零基础系列教程] 文章目录 15[存储过程和存储函数] 15.1 存储程序优缺点 15.2 存储过程的使用 15.3 存储过程的语 ...
- 数据库存储过程与存储函数
数据库存储过程与存储函数_伱糸淂忄-CSDN博客 MySQL数据库之存储过程与存储函数 - 奥辰 - 博客园 1.数据库存储过程与存储函数 存储过程是一组为了完成特定功能的SQL语句集,经过编译后存储 ...
- 【宋红康 MySQL数据库】【基础版】【15】存储过程与存储函数
文章目录 存储过程与存储函数 定义存储过程与存储函数 对比存储函数和存储过程 存储过程概述 理解 分类 创建存储过程 语法分析 代码举例 调用存储过程 调用格式 代码举例 如何调试 存储函数的使用 语 ...
- Mysql存储过程和存储函数
存储过程和存储函数 前言 存储过程的相关操作 创建 调用 查看 删除 语法 变量的定义与赋值 if条件判断 参数 case结构 while循环 repeat循环结构 loop语句 leave语句 游标 ...
- 【MySQL】存储过程与存储函数
存储过程与存储函数 1 存储过程 1.1介绍 存储过程是事先经过编译并存储在数据库中的一段SQL 语句的集合,调用存储过程可以简化应用开发人员的很多工作,减少数据在数据库和应用服务器之间的传输,对于提 ...
最新文章
- 鲲鹏入晋 万里腾飞,鲲鹏应用创新大赛2021山西赛区邀你来战!
- python实现socket编程(使用线程池)
- php多流程,多种php开发环境搭建流程
- java action的头文件注释_有没有java大神,帮我详细注释下面的代码,action里面复制来的...
- 恭喜神策数据客户趣店赴美 IPO 上市!
- mysql 字符类型
- python3 列表相关操作
- Web Application Security 网络应用程序安全 - (二)2010年网络安全威胁排行榜TOP 10...
- 一个简单的GTK的例子程序
- 2008.10 井冈山-金秋 【律动音符篇】
- 将一个数组的值逆序排列
- [MATLAB/编程]报童的诀窍/报童问题-图解法和二分法
- Scrum敏捷开发流程
- 推荐大家几款提高工作效率的软件
- 掌门教育三大举措落实个性化教学 让“因材施教”落到实处
- Jupyter Notebooks 入门
- 网站银联支付证书更换
- java 机器人模拟人工操作_Java制作自动访问网站机器人!(转)
- python全数字问题_关于python:获取一个数字的所有除数的最佳方法是什么?
- 自动剪辑视频的软件,视频自动剪辑生成软件如何剪辑视频和制作视频
热门文章
- NPM酷库:chalk,多彩的粉笔
- jar包里面文件修改
- Android 源码下载并编译Rom
- 马斯克:未来将尝试打造超级高铁运输系统 时速是飞机的2倍
- python网页版本_python 网页版
- 西威变频器avo下载调试资料_免费下载 |《西门子全集成自动化技术》,很全很详细...
- cmd 、java获取硬盘的序列号(serialnumber)物理地址 和磁盘ID逻辑地址
- 安卓TextView完美展示html格式代码
- Java框架全开源商城PC+手机版+微商城独立版+全开源系统源码
- Description: 80010105 / 服务器出现意外情况。【亲测可用】