DELIMITER $$
CREATE PROCEDURE updateUser()
begin
DECLARE row_id INT;
DECLARE row_ips_acct_no VARCHAR(100);
DECLARE row_balance decimal(20,2);
DECLARE row_freeze decimal(20,2);
DECLARE stop INT default 0;
-- 定义游标
declare rs_cursor CURSOR FOR
SELECT id, ips_acct_no, balance, freeze
FROM t_users
WHERE master_identity = 1
and ips_acct_no != '';
/*
定义游标的结束,当遍历完成时,将stop设置为null
这里也可以写成 DECLARE CONTINUE HANDLER FOR NOT FOUND SET stop = null;
*/
declare CONTINUE HANDLER FOR SQLSTATE '02000' SET stop = null;
/*开游标*/
OPEN rs_cursor;
/*游标向下走一步,将查询出来的两个值付给定义的两个变量*/
FETCH rs_cursor INTO row_id,row_ips_acct_no,row_balance,row_freeze; -- 取数据
/* 循环体 */
WHILE ( stop is not null) DO
-- 更新表
update t_users
set loan_member_role=1,
ips_loan_acct_no=row_ips_acct_no,
is_loan_bound_bank_card=1,
loan_balance=row_balance,
loan_freeze=row_freeze
where id = row_id;
/*游标向下走一步*/
FETCH rs_cursor INTO row_id,row_ips_acct_no,row_balance,row_freeze; -- 取数据
END WHILE;
/*关闭游标*/
CLOSE rs_cursor;
END$$
DELIMITER ;
CALL updateUser();