数据库之游标过程-- 基于MySQL

实例如下:

DROP PROCEDURE IF EXISTS pr_change_station_user_acct_his; -- 如果存在存储过程,即删除存储过程
create procedure pr_change_station_user_acct_his (in number int) -- 创建存储过程,存储过程的变量in表示输入参数,out表示输出参数
begin
-- 定义变量
declare cardid varchar(256);
-- 遍历数据结束标志
declare done int default false;
-- 游标
declare cur cursor for select card_id from cert_card_consume where org_id = number;
declare continue handler for not found set done = true;
-- 打开游标、开始循环
open cur;
read_loop:loop -- 游标循环
fetch cur into cardid; -- 给变量赋值
if done then
leave read_loop;
end if;
SELECT * from base_card where id =cardid;
end loop;
close cur;
end;
call pr_change_station_user_acct_his(4587)

 

对游标的理解:

例如上面的解释,游标的优点在与:

1.update更新语句

2.insert插入语句

当需要逐条变更对应的数据,假如用只用sql语句时,会使所有的更新值都变成一个值,这是就需要使用到游标,使对应的值都不一样

实例:假如需要同时更新10个update,如果用sql语句话,需要写10个子查询。但是如果使用游标的话,只需用游标一个语句给变量赋值后,就可以多次使用变量

 

posted @ 2017-07-21 15:15  木棉花的漂泊  阅读(187)  评论(0编辑  收藏  举报