首頁  >  文章  >  資料庫  >  MySQL 存储过程使用 REPEAT 来打开游标读取数据

MySQL 存储过程使用 REPEAT 来打开游标读取数据

WBOY
WBOY原創
2016-06-07 14:54:271431瀏覽

无详细内容 游标 MySQL mysqlmysqlmysql delimiter $$mysql CREATE PROCEDURE myProc (in_customer_id INT) - BEGIN - - DECLARE l_first_name VARCHAR(30); - DECLARE l_id INT; - DECLARE l_city VARCHAR(30); - DECLARE l_department_count INT; - DECLARE

游标 MySQL
mysql>
mysql>
mysql> delimiter $$
mysql> CREATE PROCEDURE myProc (in_customer_id INT)
    -> BEGIN
    ->
    ->      DECLARE l_first_name  VARCHAR(30);
    ->      DECLARE l_id          INT;
    ->      DECLARE l_city        VARCHAR(30);
    ->      DECLARE l_department_count INT;
    ->      DECLARE no_more_departments INT;
    ->
    ->      DECLARE dept_csr CURSOR FOR
    ->           SELECT id,first_name, city
    ->             FROM employee;
    ->
    ->      DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_departments=1;
    ->
    ->      SET no_more_departments=0;
    ->      OPEN dept_csr;
    ->      dept_loop:REPEAT
    ->           FETCH dept_csr INTO l_id,l_first_name,l_city;
    ->               IF no_more_departments=0 THEN
    ->                 SET l_department_count=l_department_count+1;
    ->               END IF;
    ->           select  l_id,l_first_name,l_city;
    ->      UNTIL no_more_departments
    ->      END REPEAT dept_loop;
    ->      CLOSE dept_csr;
    ->      SET no_more_departments=0;
    ->
    ->
    -> END$$
Query OK, 0 rows affected (0.00 sec)
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn