-- 如果存在存储过程test,则进行删除
DROP PROCEDURE IF EXISTS test;
-- 创建存储过程
CREATE PROCEDURE test()
-- 开始标记
BEGIN
-- 声明变量tmp,默认值为0
declare tmp int default 0;
-- 声明变量done,默认值为-1
declare done int default -1;
-- 声明游标myCursor, 游标值为user表中uid,可以有很多个
DECLARE myCursor CURSOR FOR SELECT uid FROM user;
-- 声明一个控制器
declare
continue handler for not found set done=1;
-- 打开游标
OPEN myCursor;
-- 标记
myloop : LOOP
-- 将游标的值变更到tmp变量中,如果done=1,则离开标记
FETCH myCursor INTO tmp;
if done = 1 then
leave myloop;
end if;
SET @uid = tmp;
-- 获取差额
SET @cha = (select money - u
sedmoney from money where uid = @uid);
-- 将差额和uid进行显示
SELECT @cha, @uid;
-- 修改资金表
update money set totalmoney = totalmoney + @cha where
uid = @uid;
-- 结束标记
END LOOP myloop;
-- 关闭游标
CLOSE myCursor;
END
-- 调用存储过程
CALL test();