Rechercher dans le manuel MySQL

13.6.6 Cursors

MySQL supports cursors inside stored programs. The syntax is as in embedded SQL. Cursors have these properties:

  • Asensitive: The server may or may not make a copy of its result table

  • Read only: Not updatable

  • Nonscrollable: Can be traversed only in one direction and cannot skip rows

Cursor declarations must appear before handler declarations and after variable and condition declarations.

Example:

  1. CREATE PROCEDURE curdemo()
  2.   DECLARE a CHAR(16);
  3.   DECLARE b, c INT;
  4.   DECLARE cur1 CURSOR FOR SELECT id,data FROM test.t1;
  5.   DECLARE cur2 CURSOR FOR SELECT i FROM test.t2;
  6.   DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
  7.  
  8.   OPEN cur1;
  9.   OPEN cur2;
  10.  
  11.   read_loop: LOOP
  12.     FETCH cur1 INTO a, b;
  13.     FETCH cur2 INTO c;
  14.     IF done THEN
  15.       LEAVE read_loop;
  16.     END IF;
  17.     IF b < c THEN
  18.       INSERT INTO test.t3 VALUES (a,b);
  19.     ELSE
  20.       INSERT INTO test.t3 VALUES (a,c);
  21.     END IF;
  22.   END LOOP;
  23.  
  24.   CLOSE cur1;
  25.   CLOSE cur2;

Find a PHP function

Document created the 26/06/2006, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/mysql-rf-cursors.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.

References

  1. View the html document Language of the document:en Manuel MySQL : https://dev.mysql.com/

These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.

Contents Haut