MYSQL存储过程中使用游标的实例

使用MYSQL存储过程,可以实现诸多的功能,下面将为您介绍一个MYSQL存储过程中使用游标的实例,希望对您能有所启迪。

DELIMITER $$

DROP PROCEDURE IF EXISTS getUserInfo $$

CREATE PROCEDURE getUserInfo(in date_day datetime)

— 实例
— MYSQL存储过程名为:getUserInfo
— 参数为:date_day日期格式:2008-03-08

    BEGIN
declare _userName varchar(12); — 用户名
declare _chinese int ; — 语文
declare _math int ;    — 数学
declare done int;

— 定义游标
DECLARE rs_cursor CURSOR FOR SELECT username,chinese,math from userInfo where datediff(createDate, date_day)=0;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;

— 获取昨天的日期
if date_day is null then
   set date_day = date_add(now(),interval -1 day);
end if;

open rs_cursor;
cursor_loop:loop
   FETCH rs_cursor into _userName, _chinese, _math; — 取数据
  
   if done=1 then
    leave cursor_loop;
   end if;

   — 更新表
   update infoSum set total=_chinese+_math where UserName=_userName;
end loop cursor_loop;
close rs_cursor;

    END$$

DELIMITER ;
 

以上就是MYSQL存储过程中使用游标的实例介绍。

 

 

【编辑推荐】

mysql存储过程的调用方法

MYSQL IFNULL函数的使用

MySQL日期的相关函数介绍

带您了解mysql CONCAT()函数

查看三种MySQL字符集的方法

 

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/256498.html<

(0)
运维的头像运维
上一篇2025-05-01 08:15
下一篇 2025-05-01 08:16

相关推荐

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注