13.6.6 Cursors指针

13.6.6.1 Cursor CLOSE Statement语句
13.6.6.2 Cursor DECLARE Statement语句
13.6.6.3 Cursor FETCH Statement语句
13.6.6.4 Cursor OPEN Statement语句
13.6.6.5 Restrictions on Server-Side Cursors对服务器端指针的约束

MySQL supports cursors inside stored programs. MySQL支持存储程序中的游标。The syntax is as in embedded SQL. 语法与嵌入式SQL中的相同。Cursors have these properties:游标具有以下属性:

Cursor declarations must appear before handler declarations and after variable and condition declarations.游标声明必须出现在处理程序声明之前,变量和条件声明之后。

Example:例子:

CREATE PROCEDURE curdemo()
BEGIN
  DECLARE done INT DEFAULT FALSE;
  DECLARE a CHAR(16);
  DECLARE b, c INT;
  DECLARE cur1 CURSOR FOR SELECT id,data FROM test.t1;
  DECLARE cur2 CURSOR FOR SELECT i FROM test.t2;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

  OPEN cur1;
  OPEN cur2;

  read_loop: LOOP
    FETCH cur1 INTO a, b;
    FETCH cur2 INTO c;
    IF done THEN
      LEAVE read_loop;
    END IF;
    IF b < c THEN
      INSERT INTO test.t3 VALUES (a,b);
    ELSE
      INSERT INTO test.t3 VALUES (a,c);
    END IF;
  END LOOP;

  CLOSE cur1;
  CLOSE cur2;
END;
13.6.6.1 Cursor CLOSE Statement
13.6.6.2 Cursor DECLARE Statement
13.6.6.3 Cursor FETCH Statement
13.6.6.4 Cursor OPEN Statement
13.6.6.5 Restrictions on Server-Side Cursors