「MySQL」ストアド(PROCEDURE)、カーソル(CURSOR)を使用するサンプルコード

サンプルコード:
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `cftbl`()
COMMENT 'insert table’
BEGIN
DECLARE root INT;
DECLARE zid INT;
DECLARE done INT DEFAULT FALSE;
DECLARE rs CURSOR FOR select id,tid from demo;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN rs;
read_loop: LOOP
FETCH  NEXT from rs INTO root,zid;
IF done THEN
LEAVE read_loop;
END IF;
insert into changfatbl(rootid,zid) value(root,zid);
END LOOP;

CLOSE rs;
END;;
DELIMITER ;

MySQL

Posted by arkgame