mysql存储过程中使用游标

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

局部变量需用 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 ;

      

转载于:https://www.cnblogs.com/Ting-light/p/9548356.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值