Home > Database > Mysql Tutorial > body text

使用 LOOP 和 FETCH 来读取 MySQL 游标

WBOY
Release: 2016-06-07 14:54:29
Original
1865 people have browsed it

无详细内容 游标 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();
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template