CREATE DEFINER=`root`@`%` PROCEDURE `alter_view_counts`() BEGIN #声明结束标识 DECLARE end_flag intDEFAULT0; DECLARE albumId bigint; #声明游标 album_curosr DECLARE album_curosr CURSORFORSELECT album_id FROM album; #设置终止标志 DECLARE CONTINUE HANDLER FORNOT FOUND SET end_flag=1; #打开游标 OPEN album_curosr; #遍历游标 REPEAT #获取当前游标指针记录,取出值赋给自定义的变量 FETCH album_curosr INTO albumId; #利用取到的值进行数据库的操作 UPDATE album SET album.views_count= (SELECTSUM(light_chat.views_count) FROM `light_chat` WHERE light_chat.album_id = albumId) WHERE album.album_id = albumId; # 根据 end_flag 判断是否结束 UNTIL end_flag END REPEAT; #关闭游标 close album_curosr; END