MYSQL 使用存储过程批量更新表数据

DELIMITER //
CREATE PROCEDURE `update_user`()  
BEGIN
DECLARE userId BIGINT;
DECLARE openId VARCHAR(40);
declare stop int default 0; 
DECLARE cur CURSOR FOR (SELECT open_id,uid FROM user_map);
/* 
定义游标的结束,当遍历完成时,将stop设置为null 
这里也可以写成 DECLARE CONTINUE HANDLER FOR NOT FOUND SET stop = null;
*/
declare CONTINUE HANDLER FOR SQLSTATE '02000' SET stop = null;  
   /*开游标*/  
    OPEN cur;  
    /*游标向下走一步,将查询出来的两个值付给定义的两个变量*/  
    FETCH cur INTO openId,userId;  
    /* 循环体  */  
    WHILE ( stop is not null) DO  
   /*更新数据表*/
    update user_info set  open_id=openId where uid = userId ;  
    /*游标向下走一步*/  
    FETCH cur INTO openId,userId;  
    END WHILE;
/*关闭游标*/ 
    CLOSE cur;  
END;  
//
DELIMITER ;

你可能感兴趣的:(数据库)