MYSQL存储过程中使用游标

Mysql  /  houtizong 发布于 3年前   106

DELIMITER $$

DROP PROCEDURE IF EXISTS getUserInfo $$

CREATE PROCEDURE getUserInfo(in date_day datetime)
-- 
-- 实例
-- 存储过程名为: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 ;

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客