Stored Procedure - update the table to another table in accordance with a

 

Mysql stored procedure to write, by querying a data table to another table update

For example, there are two tables roledata {role_id, role_score}, scoreinfo {role_id, score},

The following stored procedure, through a cursor, the table traversing scoreinfo scroe query value to update the table values ​​roledata in role_scroe

DROP PROCEDURE IF EXISTS `proc_updatescore`;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `proc_updatescore`()
BEGIN
	#Routine body goes here...

	DECLARE _id INT;
	DECLARE flag INT   DEFAULT true;
	DECLARE chk INT;
	DECLARE cur CURSOR FOR SELECT role_id FROM scoreinfo;
	DECLARE CONTINUE HANDLER FOR NOT FOUND SET flag = FALSE;
	OPEN cur;
	
	REPEAT
	FETCH cur INTO _id;
	SELECT COUNT(*) INTO chk FROM roledata WHERE roledata.role_id=_id;
	IF chk = 1 THEN
	update roledata,scoreinfo SET roledata.role_score= (SELECT score FROM scoreinfo WHERE role_id=_id) WHERE roledata.role_id=_id;
	END IF;
	UNTIL flag = FALSE
	END REPEAT ;
	CLOSE cur ;
END
;;
DELIMITER ;

  

 

Guess you like

Origin www.cnblogs.com/bodboy/p/11319907.html