存储过程概念: 就是一组为了完成特定功能的sql语句集创建存储过程
【没有参数】drop procedure if exists 名字;delimiter $$create procedure 名字()beginsql代码块end;delimiter $$【有参数】in 参数传进来  drop procedure if exists 名字;delimiter $$create procedure 名字(in 参数1 数据类型1,out 参数2 数据类型2....)beginsql代码块 end;delimiter $$set @b = "";call  名字(值1,@b);
********************************************************************************调用:call 名字();call 名字;自定义函数是解决特定的需求 存储过程会比自定函数更强大。drop procedure if exists xiaoyang;delimiter $$create procedure xiaoyang()beginselect "虫虫你好漂亮!!";end $$delimiter ;drop procedure if exists xiaoyang2;delimiter $$create procedure xiaoyang2(in n1 int(11),out n2 varchar(100))beginif(n1 <100)then set n2 := concat(n1,"<100");elseif n1 = 100then set n2 := concat(n1,"=100");else set n2 := concat(n1,">100");end if;end $$delimiter ;set @m = "";call xiaoyang2(100,@m);select @m;自定义函数里面不能返回结果集。【select】
存储过程可以赋值:select 值 into  变量;set 变量 = 值;set 变量 := 值;  [推荐方式]
********************************************************************************查看存储过程
show procedure status;
或
select db, name, type from mysql.proc where db = 'test' and type='PROCEDURE';
********************************************************************************显示创建存储过程的语句
show create procedure p1\G
********************************************************************************删除存储过程
drop procedure if exists p1;
********************************************************************************在存储过程里边可以使用declare声明变量
格式: declare 变量名 变量类型 [default 默认值];drop procedure if exists p2;
delimiter $$create procedure p2()begindeclare age int default 20;declare name varchar(30) default '狗蛋';select concat('姓名:', name, '     年龄:', age);end$$
delimiter ;
call p2();
********************************************************************************可以数算运算
delimiter $$create procedure p3()begindeclare age int default 40;set age := age + 10;           #赋值select age from dual;end$$
delimiter ;
call p3();
********************************************************************************可以使用逻辑判断语句
判断语法:if 要判断的语句 then要执行的sql(statement)else要执行的sql(statement)end if;drop procedure if exists p4;
delimiter $$create procedure p4()begindeclare age int default 20;if age > 60 thenselect '已经老了' from dual;elseselect '一枝花' as '威廉' from dual;end if;end$$
delimiter ;
call p4();
********************************************************************************还可以给存储过程传参
存储过程里括号可以用来声明参数
语法: [in | out | inout] 参数名 参数数据类型delimiter $$create procedure p5(u_name varchar(30), u_age int)beginif u_name = '胖胖' thenselect '其实一点也不胖' as '胖胖';elseif u_name = '小红' thenselect '她真的很好看' as '小红';elseselect '这个我就不知道了' as '未知的';end if;end$$
delimiter ;
call p5('胖胖', 18);
call p5('小红', 18);
call p5('狗剩', 18);
********************************************************************************可以使用循环
--在存储过程里使用循环求 1 到 10 的和;
delimiter $$create procedure p6()begindeclare num int default 0;declare i int default 0;while i <= 10 doset num := num + i;set i := i + 1;end while;select num as '总和';end$$
delimiter ;
call p6();
********************************************************************************参数 [in | out | inout] 的使用
语法: [in | out | inout] 参数名 参数数据类型--in
--求1到指定值的和
drop procedure if exists p7;
delimiter $$create procedure p7(in n int)begindeclare num int default 0;declare i int default 0;while i <= n doset num := num + i;set i := i + 1;end while;select num as '总和';end$$
delimiter ;
call p7(100);--out
--需求: 把计算出来的和返回出来
drop procedure if exists p8;
delimiter $$create procedure p8(in n int, out num int)begindeclare i int default 0;set num := 0;while i <= n doset num := num + i;set i := i + 1;end while;end$$
delimiter ;
call p8(100, @value);    @name 表示全局的用户变量
select @value as '总和';--inout
delimiter $$create procedure p9(inout age tinyint unsigned)beginset age := age +18;end$$
delimiter ;
set @value = 10;
call p9(@value);
select @value;
********************************************************************************循环(repeat)
drop procedure if exists p10;
delimiter $$create procedure p10()begindeclare i int default 100;# repeatwhile i < 10 doselect i as 'i';set i := i + 1;end while;# until i > 10 end repeat;    #意思是在 i的值 大于10之前 这个条件就满足end$$
delimiter ;
call p10();
repeat循环是先执行再判断
while循环是先判断再执行
********************************************************************************可以使用case 语法
delimiter $$create procedure p11(in u_name varchar(30))begincase u_namewhen '狗蛋' thenselect '狗蛋一点也不蛋' as '狗蛋';when '狗剩' thenselect '这是狗蛋他哥' as '狗剩';when '铁蛋' thenselect '这狗蛋他老表' as '铁蛋';elseselect '不知道他是谁了' as '未知的';end case;end$$
delimiter ;
call p11('铁蛋');
call p11('狗剩');
call p11('蛋蛋');
**********************************************************************************游标
一条sql,对应n着条结果集的资源, 取出资源的接口/句柄, 就是游标沿着游标可以一次取出一行
--需求: 使用游标获取到第一行的数据;delimiter $$create procedure p12()begindeclare row_no int;declare row_name varchar(30);declare row_sex varchar(10);declare getstu cursor for select stu_no, stu_name, stu_sex from student;     #定义一个游标open getstu;  #打开游标fetch getstu into row_no, row_name, row_sex;      #获取游标select row_no, row_name, row_sex;close getstu;  #关闭游标end$$
delimiter ;
call p12();
********************************************************************************--需求: 使用游标获取到所有的数据;
drop procedure if exists p13;
delimiter $$create procedure p13()begindeclare row_no int;declare row_name varchar(30);declare row_sex varchar(10);declare row int default 0;declare i int default 0;declare getstu cursor for select stu_no, stu_name, stu_sex from student;     #定义一个游标select count(*) into row from student;open getstu;  #打开游标repeatfetch getstu into row_no, row_name, row_sex;      #获取游标select row_no, row_name, row_sex;set i := i + 1;until i >= row end repeat;close getstu;  #关闭游标end$$
delimiter ;
call p13();
********************************************************************************--declare continue handler for NOT FOUND的使用drop procedure if exists p14;
delimiter $$create procedure p14()begindeclare row_no int;declare row_name varchar(30);declare row_sex varchar(10);declare i int default 0;declare getstu cursor for select stu_no, stu_name, stu_sex from student where stu_no = 1;     #定义一个游标# declare continue handler for NOT FOUND set i := 1;      #声明如果没有返回时程序会继续执行 并且把i的值设置为1;declare exit handler for NOT FOUND set i := 1;            #exit 退出open getstu;  #打开游标repeatfetch getstu into row_no, row_name, row_sex;      #获取游标# if i = 0 thenselect row_no, row_name, row_sex, i;# end if;until i = 1 end repeat;      close getstu;  #关闭游标end$$
delimiter ;
call p14();
游标 :遍历整个表 ,对表做出一些改变1. 声明游标   declare `名字` cursor for 表名;  【select id,name from xiaomo】  ======打造宝剑eg: declare xiaoyang cursor for select id,name from xiaomo;2. 打开游标 open 游标; eg: open xiaoyang;    # 取出数据    =====  拔出宝剑3. 取数据 fetch 游标的名字 into 变量1,变量2...;   eg: fetch xiaoyang into mid,mname;  ======寻找敌人,斩于剑下脏话:if(变量1 = "脏话1" or 变量1.... .... ...)then update xiaomo set manme = "*****" where id = 变量1;4.关闭游标 close 游标的名字;     ===== 宝剑回樵eg: close xiaoyang;来一个入门案例:+----+------------+------+------+------+-------+
| id | name       | age  | sex  | xpri | money |
+----+------------+------+------+------+-------+
|  1 | I love you |    8 | NULL | NULL |  NULL |
| 12 | 风度迷彩   |   17 | NULL | NULL |   600 |
| 13 | 虫虫       |   27 | NULL | NULL |   600 |
+----+------------+------+------+------+-------+需求:如果用户小于10岁,更改name为  如果用户大于10岁小于18岁,更改name为  我是未成年,你别骗我如果他大于18岁, 更改name为   我是成年人,知道潜规则drop procedure if exists mypro;delimiter $$create procedure mypro()begindeclare mid int(11); #声明一个变量mid,数据类型是 int(11)declare mage int(11); declare mname char(80);declare xiaotao cursor for select id,age,name from xiaomo; #声明游标,名字为xiaotao,遍历的是xiaomo表的id和age字段。open xiaotao;  #打开游标fetch xiaotao into mid,mage,mname;  #从游标里面取出数据,把id取到的值赋给mid,把age取到的值赋给mageif(mage<10)then update xiaomo set name = concat(mname,"我是小孩子,读书少") where id = mid;end if;fetch xiaotao into mid,mage,mname;  #从游标里面取出数据,把id取到的值赋给mid,把age取到的值赋给mageif(mage>10 and mage<18)then update xiaomo set name = concat(mname,"我是未成年,你别骗我") where id = mid;end if;fetch xiaotao into mid,mage,mname;  #从游标里面取出数据,把id取到的值赋给mid,把age取到的值赋给mageif(mage>18)then update xiaomo set name = concat(mname,"我是成年人,知道潜规则") where id = mid;end if;close xiaotao;end $$delimiter ;mysql> select * from xiaomo;
+----+------------------------------------------------+------+------+------+-------+
| id | name                                           | age  | sex  | xpri | money |
+----+------------------------------------------------+------+------+------+-------+
|  1 | I love you我是小孩子,读书少我是小孩子,读书少 |    8 | NULL | NULL |  NULL |
| 12 | 风度迷彩我是未成年,你别骗我                   |   17 | NULL | NULL |   600 |
| 13 | 虫虫我是成年人,知道潜规则                     |   27 | NULL | NULL |   600 |
+----+------------------------------------------------+------+------+------+-------+mysql> call mypro;
+------+------+
| id号 | 年龄 |
+------+------+
|    1 |    8 |
+------+------+mysql> call mypro;
+------+------+
| id号 | 年龄 |
+------+------+
|    1 |    8 |
+------+------+
1 row in set (0.00 sec)+------+------+
| id号 | 年龄 |
+------+------+
|   12 |   17 |
+------+------+
1 row in set (0.01 sec)+------+------+
| id号 | 年龄 |
+------+------+
|   13 |   27 |
+------+------+
1 row in set (0.01 sec)循环repeat业务代码until 条件  【退出】end repeat;存储过程第一次加载到内存的时候慢,以后调用的时候就好啦。改进版本
drop procedure if exists mypro;delimiter $$create procedure mypro()begindeclare mid int(11); #声明一个变量mid,数据类型是 int(11)declare mage int(11); declare mname char(80);declare flag int default true;  #申明一个变量,默认值是为truedeclare xiaotao cursor for select id,age,name from xiaomo; #声明游标,名字为xiaotao,遍历的是xiaomo表的id和age字段。declare continue handler for not found set flag = false; #遍历没有数据,就更改标志flag为false# declare continue handler for not found 条件;open xiaotao;  #打开游标repeatfetch xiaotao into mid,mage,mname;  #从游标里面取出数据,把id取到的值赋给mid,把age取到的值赋给mageif(mage<10)then update xiaomo set name = concat(mname,"我是小孩子,读书少") where id = mid;elseif(mage>10 and mage<18)then update xiaomo set name = concat(mname,"我是未成年,你别骗我") where id = mid;elseupdate xiaomo set name = concat(mname,"我是成年人,知道潜规则") where id = mid;end if;until flag = falseend repeat;close xiaotao;  # 关闭游标end $$delimiter ;call mypro;call mypro();mysql> select * from xiaomo;
+----+------------------------------------------------------------------+------+------+------+-------+
| id | name                                                             | age  | sex  | xpri | money |
+----+------------------------------------------------------------------+------+------+------+-------+
|  1 | I love you我是小孩子,读书少我是小孩子,读书少我是小孩子,读书少 |    8 | NULL | NULL |  NULL |
| 12 | 风度迷彩我是未成年,你别骗我我是未成年,你别骗我                 |   17 | NULL | NULL |   600 |
| 13 | 虫虫我是成年人,知道潜规则我是成年人,知道潜规则                 |   27 | NULL | NULL |   600 |
+----+------------------------------------------------------------------+------+------+------+-------+drop function if exists mypro;delimiter $$create function mypro()returns varchar(100)begindeclare mid int(11); #声明一个变量mid,数据类型是 int(11)declare mage int(11); declare mname char(80);declare flag int default true;  #申明一个变量,默认值是为truedeclare xiaotao cursor for select id,age,name from xiaomo; #声明游标,名字为xiaotao,遍历的是xiaomo表的id和age字段。declare continue handler for not found set flag = false; #遍历没有数据,就更改标志flag为false # declare continue handler for not found 条件;open xiaotao;  #打开游标repeatfetch xiaotao into mid,mage,mname;  #从游标里面取出数据,把id取到的值赋给mid,把age取到的值赋给mageif(mage<10)then update xiaomo set name = concat(mname,"我是小孩子,读书少") where id = mid;elseif(mage>10 and mage<18)then update xiaomo set name = concat(mname,"我是未成年,你别骗我") where id = mid;elseupdate xiaomo set name = concat(mname,"我是成年人,知道潜规则") where id = mid;end if;until flag = falseend repeat;close xiaotao;  # 关闭游标return "ok";end $$delimiter ;ERROR 1406 (22001): Data too long for column 'name' at row 11406 : error-code  错误代码22001 :  sqlstate   错误状态mysql> select * from xiaomo;
+----+--------+------+------+------+-------+
| id | name   | age  | sex  | xpri | money |
+----+--------+------+------+------+-------+
|  1 | gezi1  |    8 | NULL | NULL |  NULL |
| 12 | gezi12 |   17 | NULL | NULL |   600 |
| 13 | gezi13 |   27 | NULL | NULL |   600 |
+----+--------+------+------+------+-------+ERROR 1406 (22001): Data too long for column 'name' at row 1ERROR 1329 (02000): No data - zero rows fetched, selected, or processed自定义conditiondeclare mycondition condition for 错误代码; eg:  declare mycondition condition for 1406;declare mycondition condition for 错误状态; eg:  declare mycondition condition for '22001';drop procedure if exists mpro;delimiter $$create procedure mpro()begin declare mid int(11);declare mname varchar(100);declare flag int default true;declare mycondition condition for 1406;  #  错误状态declare mycondition1 condition for 1259;  #  错误状态declare mycondition2 condition for 1406;  #  错误状态declare mycondition3 condition for 1406;  #  错误状态declare mycondition4 condition for 1406;  #  错误状态declare mycondition5 condition for 1406;  #  错误状态declare mycondition6 condition for 1406;  #  错误状态 declare cur cursor for select id,name from xiaomo;if(.....)thendeclare continue handler for mycondition set flag = false;   # not found 属于一种状态【condition】elseif(/.///)then declare continue handler for mycondition set flag = false;   # not found 属于一种状态【condition】open cur;repeatfetch cur into mid,mname;update xiaomo set name = concat(name,"hello!!") where id = mid;until flag =falseend repeat;close cur;end $$delimiter ;drop procedure if exists mpro;delimiter $$create procedure mpro()begin declare mid int(11);declare mname varchar(100);declare flag int default true;declare mycondition condition for '22001';  #  错误状态declare cur cursor for select id,name from xiaomo;declare continue handler for mycondition set flag = false;   # not found 属于一种状态【condition】open cur;repeatfetch cur into mid,mname;update xiaomo set name = concat(name,"hello!!") where id = mid;until flag =falseend repeat;close cur;end $$delimiter ;600万create table bw(id int(11) primary key auto_increment,name varchar(11),age tinyint(4) unsigned,birth timestamp);timestamp: 时间蹉while 条件 dosql代码end while;-----------------------------------------
默认引擎
| bw    | CREATE TABLE `bw` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(11) DEFAULT NULL,`age` tinyint(4) unsigned DEFAULT NULL,`birth` timestamp NOT NULL DEFAULT CURRPRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |innodb 索引的情况主键索引  聚簇索引=====》 行数据页面分裂------------------------------------------修改表的引擎
alter table bw engine myisam;bw    | CREATE TABLE `bw` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(11) DEFAULT NULL,`age` tinyint(4) unsigned DEFAULT NULL,`birth` timestamp NOT NULL DEFAULT CURRENPRIMARY KEY (`id`)ENGINE=MyISAM DEFAULT CHARSET=utf8 |插入1000条数据drop procedure if exists mypro;delimiter $$create procedure mypro()begindeclare i int(11) default 0;declare mage int(11) default 10;while i<1000 doset i:= i+1;set mage = floor(rand()*110);  # 0 110insert into bw(name,age) values(concat("monkey",i),mage);end while;       end $$delimiter ;mysql> call mypro;Query OK, 1 row affected (0.03 sec)mysql> call mypro();Query OK, 1 row affected (27.74 sec)

mysql_存储过程游标、循环相关推荐

  1. mysql 存储过程游标删除_mysql数据库存储过程游标循环,提前退出

    需求:向trade这个数据库中的每一个表增加多个字段 遇到问题:存储过程,游标,循环,动态sql执行 注意: mysql 存储过程在我所使用的5.5版本中不能使用 show 的命令,利用 inform ...

  2. mysql 存储过程游标 循环输出select 查询结果

    CREATE DEFINER=`ceshi`@`%` PROCEDURE `A1_test`( ) BEGIN -- 创建计数器DECLARE s int DEFAULT 0; -- 用来接字段 有几 ...

  3. mysql 存储过程声明式游标_Mysql 存储过程中使用游标循环读取临时表

    游标 游标(Cursor)是用于查看或者处理结果集中的数据的一种方法.游标提供了在结果集中一次一行或者多行前进或向后浏览数据的能力. 游标的使用方式 定义游标:Declare 游标名称 CURSOR ...

  4. MySQL存储过程使用游标循环数据列表

    本篇文章主要讲解,我的一个案例,使用存储过程和游标循环数据列表,并且做一些操作,比如保存一些数据,修改一些数据: 1.需求? MySQL使用存储过程循环数据列表? 2.先描述下MySQL有哪些循环的语 ...

  5. mysql 游标循环_mysql for循环遍历 游标用处

    以前一直不知道游标的作用,之前的结果集遍历 博主一直用的是记录查询出来的记录总数并一条条用LIMIT 1 OFFSET index 进行查找,并进行处理的. 现在终于会使用游标了,献上一个小例子供大家 ...

  6. MySQL存储过程 游标

    MySQL存储过程  游标 如何在存储过程中使用MySQL游标来遍历SELECT语句返回的结果集 MySQL游标简介 要处理存储过程中的结果集,请使用游标.游标允许您迭代查询返回的一组行,并相应地处理 ...

  7. mysql的游标处理_MySQL存储过程 游标 错误处理的示例代码

    MySQL存储过程 游标 错误处理的示例代码--set_account_data 重新生成用户编号 BEGIN DECLARE temp_id INT(8); /*用户id*/ DECLARE tem ...

  8. MySQL存储过程-循环遍历查询到的结果集

    MySQL存储过程-循环遍历查询到的结果集 1.创建存储过程 (更好的阅读体验,请移步我的个人博客)根据MySQL的语法创建存储过程,要注意的是如果循环遍历查询到的结果集,取出结果集中的数据做操作. ...

  9. mysql 利用游标循环删除数据报错:No data - zero rows fetched, selected, or processed

    第一次用mysql的游标来循环处理数据,编写的存储过程如下: DROP PROCEDURE IF EXISTS delete_Menu; DELIMITER $$ CREATE PROCEDURE d ...

  10. SqlServer存储过程中循环的使用

    1.while循环 格式示例如下: declare @i int set @i=1 while @i<30 begin insert into test (userid) values(@i) ...

最新文章

  1. 随堂练习--找水王2
  2. MyBatis中提示:You have an error in your SQL syntax; check the manual that corresponds to your MySQL ser
  3. ant的设置properties
  4. 如何从ios酷我音乐盒中导出已下载的音乐文件(使用Java编程实现)
  5. 如何制定好的方案之四:执行力是决定因素
  6. 什么是python自动化测试_python已经自动化了,大家一般用什么测试框架?
  7. 日本东北大学改进单阶段人脸检测—兼具速度与精度优势
  8. ”易书网“开发总结——管理篇
  9. mysql 多表联合查询怎么一行显示_使用 explain 优化你的 mysql 性能
  10. 【生信进阶练习1000days】day10-vcf format
  11. 付呗聚合支付快速教程 基础篇②——FubeiUtils付呗工具类(封装参数和签名规则)
  12. 新一代物联网商用全面铺开 NB-IoT擎起新智慧城市
  13. 如何在电脑上安装虚拟机和系统。全网最全教程,不接受反驳。
  14. Cannot subclass final class org.springframework.boot.autoconfigure.AutoConfigurationPackages
  15. Array.of()
  16. 【计组】字长、数据总线、地址总线
  17. 刀片服务器改台式电脑_刀片服务器安装指南_IT /计算机_信息
  18. 机器学习理论-PAC learning
  19. ruoyi-cloud 服务器端idea启动报错Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:exec (defau
  20. 如何近似计算回归方程的预测区间?

热门文章

  1. java 断点跳到注释,一个空指针异常,代码如下,打了断点,一到“TOPICID”那里(在下方注释4那里)就抛异常-_-||...
  2. [瞎搞]Lucas定理证明
  3. JavaWEB10:Request、Response
  4. android 状态栏高度多少像素,Android之获取屏幕的尺寸像素及获取状态栏标题栏高度...
  5. Robust Classification with Convolutional Prototype Learning
  6. 基于产生式规则的动物识别系统(Python)
  7. 【原创】聊天机器人与自动问答技术
  8. scikit-learn回归类库使用
  9. 安科瑞智能照明控制系统在医院行业的应用
  10. MT【305】丹德林双球