当前位置:Gxlcms > mysql > 使用LOOP和FETCH来读取MySQL游标

使用LOOP和FETCH来读取MySQL游标

时间:2021-07-01 10:21:17 帮助过:110人阅读

无详细内容 游标 MySQL mysqlmysql delimiter $$mysql CREATE PROCEDURE myProc() - BEGIN - DECLARE l_sale_id INT; - DECLARE l_last_sale INT DEFAULT 0; - - DECLARE sale_csr CURSOR FOR - SELECT id - FROM employee; - - DECLARE CONTINUE HANDLER FOR

<无详细内容> 游标 MySQL
mysql>
mysql> delimiter $$
mysql> CREATE PROCEDURE myProc()
    -> BEGIN
    ->   DECLARE l_sale_id INT;
    ->   DECLARE l_last_sale INT DEFAULT 0;
    ->
    ->   DECLARE sale_csr CURSOR FOR
    ->     SELECT id
    ->       FROM employee;
    ->
    ->   DECLARE CONTINUE HANDLER FOR NOT FOUND SET l_last_sale=1;
    ->
    ->   OPEN sale_csr;
    ->   sale_loop:LOOP
    ->     FETCH sale_csr INTO l_sale_id;
    ->     IF l_last_sale THEN
    ->       LEAVE sale_loop;
    ->     END IF;
    ->     select l_sale_id;
    ->   END LOOP sale_loop;
    ->   CLOSE sale_csr;
    ->
    -> END$$
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> delimiter ;
mysql> call myProc();

人气教程排行