用户变量一般以@开头,作用于全局范围

局部变量需用 declare 定义格式为 declare 变量名 数据类型 [default value];

mysql 数据类型有 int ,float,date,varchar(length)等

声明的顺序必须是 先声明变量,再声明游标、最后声明handler。

同一个存储过程中,一个游标的使用和两个游标的使用是一样的。

调用存储过程 call sp_name();

查询某数据库中全部存储过程 :

select name from mysql.proc where db='数据库名';

select routine_name from information_schema.routines where routine_schema='数据库名';

show procedure status where db='数据库名';

查看单个存储过程: show create procedure 数据库.存储过程名;

删除存储过程 :drop procedure 存储过程名

存储过程创建语句:

delimiter $$                                                          -- 定义语句结束标志为 $$, 默认结束标志是;

drop procedure if exists test.sp_example$$           -- 创建存储过程关键字

create procedure test.sp_example()                     -- 创建存储过程具体内容:

begin                                                                     -- 存储过程内容以begin开始,end 结束。

declare _inner_code int;                                       --  声明 局部变量 及变量类型

declare _writedate date;

declare _done int default 1;                                  -- 声明 局部变量 、变量类型  及 变量默认值

declare c_cursor cursor for select inner_code,writedate from test.example group by inner_code,writedate;

-- 声明游标名、游标所存储数据

-- 此处可继续申明第二个游标 : declare a_cursor cursor for select  ... from ...;

declare continue handle for not found set _done=0; --  当出现 not found 的错误时 continue 并将变量_done的值设置为0

start transaction;

open c_cursor;                                                     -- 打开游标

fetch c_cursor into _inner_code,_writedate;

-- 获取当前游标指向的数据行赋值给变量_inner_code,_writedate,并将游标指向下一行

while _done do

功能语句块

fetch c_cursor into _inner_code,_writedate;

/* 获取当前游标指向的数据行赋值给变量_inner_code,_writedate,并将游标指向下一行,当游标已经指向最后一行时会造成游标溢出. mysql 中游标溢出时会引发mysql预定义的not found 错误,在上面定义了一个continue属性的操作handle,当出现not found 错误时 继续,并修改_done变量的值为0,使循环结束*/

end while ;

close c_cursor ;                                                  --  关闭游标

end $$

delimiter ;                                                           -- 将结束标志定义回;

游标嵌套

在mysql中同一个error事件只能定义一次,如果多定义的话在编译时会提示 duplicate handler declared in the same block.

每个begin end 块都是一个独立的scope 区域,嵌套的游标可用begin end 包裹。

drop procedure if exists nest_use;

create procedure nest_use()

begin

declare _n varchar(20);

declare done int default false;

declare cur cursor for select age from store group by age;

declare continue handler for not found set done =true;

open cur ;

read_loop:loop

fetch cur into _n;

if done then

leave read_loop;

end if ;

begin

declare c int ;

declare n varchar(20);

declare total int default 0;

declare done int default false;

declare cur cursor for select name ,count from store where name='iphone';

declare continue handler for not found set done=true;

set total=0;

open cur ;

iphone_loop:loop

fetch cur into n ,c ;

if done then

leave iphone_loop;

end if ;

set total =tatal + c;

end loop;

close cur;

select _n,n,total;

end;

begin

declare c int;

declare n varchar(20);

declare total int default 0;

declare done int default false;

declare cur cursor for select name,count from store where name = 'android';

declare continue HANDLER for not found set done = true;

set total = 0;

open cur;

android_loop:loop

fetch cur into n,c;

if done then

leave android_loop;

end if;

set total = total + c;

end loop;

close cur;

select _n,n,total;

end;

end loop;

close cur;

end ;

mysql存储过程中使用游标的更多相关文章

  1. mysql 存储过程中使用游标中使用临时表可以替代数组效果

    mysql不支持数组.但有时候需要组合几张表的数据,在存储过程中,经过比较复杂的运算获取结果直接输出给调用方,比如符合条件的几张表的某些字段的组合计算,mysql临时表可以解决这个问题.临时表:只有在 ...

  2. Mysql 存储过程中使用多游标

    Mysql 存储过程中使用多游标 drop procedure IF EXISTS test_proc_1; create procedure test_proc_1() begin ; ) ; ) ...

  3. MySQL存储过程中的3种循环,存储过程的基本语法,ORACLE与MYSQL的存储过程/函数的使用区别,退出存储过程方法

    在MySQL存储过程的语句中有三个标准的循环方式:WHILE循环,LOOP循环以及REPEAT循环.还有一种非标准的循环方式:GOTO,不过这种循环方式最好别用,很容易引起程序的混乱,在这里就不错具体 ...

  4. MYSQL存储过程中常使用的命令记录

    MYSQL存储过程中常使用的命令记录 1.触发器trigger 查看:show triggers; 2.存储过程procedure 查看:show procedure status; 查看详细:sho ...

  5. MYSQL存储过程中的IN、OUT和INOUT

    MYSQL存储过程中的IN.OUT和INOUT,不能简单理解为一个方法的参数和返回值,而是面向整个过程上下文变量的. 一.MySQL 存储过程参数(in) 基本可以理解为传入function的参数,而 ...

  6. MySQL存储过程中使用SELECT …INTO语句为变量赋值

    使用SELECT …INTO语句为变量赋值 在MySQL存储过程中,可以使用SELECT …INTO语句对变量进行赋值,该语句在数据库中进行查询,并将得到的结果赋值给变量.SELECT …INTO语句 ...

  7. 【转】MySQL存储过程中使用动态行转列

    MySQL存储过程中使用动态行转列 最近做项目关于数据报表处理,然而数据库存储格式和报表展现形式不同,需要进行一下行转列的操作,在做上一个项目的时候也看了一下,但是后来换了读取方式,也就没深入研究这个 ...

  8. mysql -- 存储过程中 declare 和 set 定义变量的区别

    mysql存储过程中,定义变量有两种方式:1.使用set或select直接赋值,变量名以 @ 开头.例如:set @var=1;可以在一个会话的任何地方声明,作用域是整个会话,称为会话变量. 2.以 ...

  9. mysql存储过程中 乱码问题解决办法

    中文乱码无论在何时都是一个头疼的问题,mysql的存储过程参数也同样存在这个问题.1.直接使用insert into语句没问题,能够正常插入汉字.2.把insert into语句移到Procedure ...

随机推荐

  1. 数据结构(C语言版)-第8章 排序

    8.1 概述 1. 什么是排序?  将一组杂乱无章的数据按一定规律顺次排列起来. 2. 排序的目的是什么? ——便于查找! 3. 什么叫内部排序?什么叫外部排序? 若待排序记录都在内存中,称为内部排序 ...

  2. Getting Started with Processing 第五章的easing问题(2)

    程序代码清单如下: float x; float y; float px; float py; float easing = 0.05; void setup(){ size(480,120); st ...

  3. English trip V1 - B 5.Is It Cold Outside? 外面很冷? Teacher:Corrine Key: weather

    In this lesson you will learn to talk about the weather. 本节课将学习到关于天气 课上内容(Lesson) 词汇(Key Word ) # 关于 ...

  4. Docker入门(CentOS7)

    1.docker安装yum -y install bash-completion     #tab键补齐包yum install net-tools -yyum -y install wgetwget ...

  5. LeetCode--242--有效的字母异位词

    问题描述: 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词. 示例 1: 输入: s = "anagram", t = "nagara ...

  6. p2751 Job Processing

    如果单单只安排过程1的时间最短,很容易算出来.用优先队列取最小,加上增量后再放回就行.对过程2也进行这样的操作.将过程1第一个完成的在过程2最后一个完成.以样例来说,过程1:1,1,2,2,3,过程2 ...

  7. Linux 权限管理命令

    第四章(二)权限管理命令 Linux常用命令(二)权限管理命令

  8. dsu on tree练习

    dsu on tree主要是处理一些有根树子树询问的操作, 作用与点分治和线段树合并类似. 一般无根树询问所有树链信息的直接就点分了, 有根树的话一般用线段树合并或dsu on tree, 线段树合并 ...

  9. http认证方式,工程部分实现

    学习过程中,被boss批评,要求去复习http协议,因此找了相关资料做成一个系列:对于http认证方式不清楚的可以参考我的上一篇文章 http认证方式https://www.cnblogs.com/j ...

  10. 『计算机视觉』Mask-RCNN_推断网络其五:目标检测结果精炼

    一.Detections网络 经过了ROI网络,我们已经获取了全部推荐区域的信息,包含: 推荐区域特征(ROIAlign得到) 推荐区域类别 推荐区域坐标修正项(deltas) 再加上推荐区域原始坐标 ...