mysql游标

DELIMITER $$

CREATE
    /*[DEFINER = { user | CURRENT_USER

}]*/
    PROCEDURE `phoenix_user`.`curr_tb`()
   

/*LANGUAGE SQL
    | [NOT] DETERMINISTIC
    | { CONTAINS

SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
    |

SQL SECURITY { DEFINER | INVOKER }
    | COMMENT

'string'*/
    BEGIN

    /*局部变量的定义 declare*/

         DECLARE userId BIGINT(11) DEFAULT 0 ;

         DECLARE cur1 CURSOR FOR SELECT id FROM tb_user;


         DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET

userId = -1;
 

     OPEN cur1;


         FETCH cur1 INTO userId;

         /* 循环体 这很明显 把游标查询出的 name 都加起并用

; 号隔开 */

      WHILE ( userId <> -1) DO

INSERT INTO tb_help_message (userId,TYPE,remark)

VALUES(userId,1,'hello world');


        /*游标向下走一步*/

        FETCH cur1 INTO userId;

      END WHILE;

      CLOSE cur1;

    END$$

DELIMITER ;


CALL curr_tb();

你可能感兴趣的:(mysql)