基于MySQL游标的具体使用详解

来源:互联网 发布:js li取消隐藏 编辑:程序博客网 时间:2024/06/02 05:11

转自:http://www.jb51.net/article/38317.htm
游标完整使用实例:

create table m1.user(name varchar(200));insert into m1.user values ("lily");insert into m1.user values ("lucy");insert into m1.user values ("monica");insert into m1.user values ("tom");insert into m1.user values ("jane");select * from m1.user;drop procedure if exists useCursor;create procedure useCursor()begindeclare tmpName varchar(200) default '';declare allName varchar(200) default '';declare cur1 cursor for select name from m1.user;declare continue handler for sqlstate '02000' set tmpName = null;open cur1;fetch cur1 into tmpName;while (tmpName is not null) doset tmpName = CONCAT(tmpName,";");fetch cur1 into tmpName;end while;select allName;end;call useCursor();drop procedure if exists useCursor;drop table m1.user;

测试表 level ;

create table test.level (name varchar(20));

再 insert 些数据 ;
代码
初始化

drop procedure if exists useCursor //

建立 存储过程 create

CREATE PROCEDURE useCursor()BEGIN

局部变量的定义 declare

declare tmpName varchar(20) default '' ;  declare allName varchar(255) default '' ;  declare cur1 CURSOR FOR SELECT name FROM test.level ;  

MySQL 游标 异常后 捕捉
并设置 循环使用 变量 tmpname 为 null 跳出循环。

declare CONTINUE HANDLER FOR SQLSTATE '02000' SET tmpname = null; 

开游标

OPEN cur1;

游标向下走一步

FETCH cur1 INTO tmpName;

循环体 这很明显 把MySQL 游标查询出的 name 都加起并用 ; 号隔开

WHILE ( tmpname is not null) DO set tmpName = CONCAT(tmpName ,";") ; set allName = CONCAT(allName ,tmpName) ; 

游标向下走一步

FETCH cur1 INTO tmpName;

结束循环体:

END WHILE;

关闭游标

CLOSE cur1;

选择数据

select allName ;

结束存储过程

END;//

调用存储过程:
复制代码 代码如下:

call useCursor()//

运行结果:

mysql> call useCursor()//+--------------------------------------+| allName                              |+--------------------------------------+| f1;c3;c6;c5;c2;c4;c1;f1;f3;f4;f2;f5; |+--------------------------------------+1 row in set (0.00 sec)

loop循环游标:

DELIMITER $$  DROP PROCEDURE IF EXITS cursor_example$$  CREATE PROCEDURE cursor_example()       READS SQL DATA  BEGIN       DECLARE l_employee_id INT;       DECLARE l_salary NUMERIC(8,2);       DECLARE l_department_id INT;       DECLARE done INT DEFAULT 0;       DECLARE cur1 CURSOR FOR SELECT employee_id, salary, department_id FROM employees;       DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;       OPEN cur1;       emp_loop: LOOP           FETCH cur1 INTO l_employee_id, l_salary, l_department_id;           IF done=1 THEN               LEAVE emp_loop;           END IF;       END LOOP emp_loop;       CLOSE cur1;  END$$  DELIMITER ;  

repeat循环游标:

/*创建过程*/DELIMITER //DROP PROCEDURE IF EXISTS test //CREATE PROCEDURE test()BEGIN    DECLARE done INT DEFAULT 0;    DECLARE a VARCHAR(200) DEFAULT '';    DECLARE c VARCHAR(200) DEFAULT '';    DECLARE mycursor CURSOR FOR SELECT  fusername FROM uchome_friend;    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;    OPEN mycursor;    REPEAT         FETCH mycursor INTO a;        IF NOT done THEN            SET c=CONCAT(c,a);/*字符串相加*/        END IF;    UNTIL done END REPEAT;    CLOSE mycursor;    SELECT c;END //DELIMITER ;
/*创建过程*/DELIMITER //DROP PROCEDURE IF EXISTS test //CREATE PROCEDURE test()BEGIN    DECLARE done INT DEFAULT 0;    DECLARE a VARCHAR(200) DEFAULT '';    DECLARE c VARCHAR(200) DEFAULT '';    DECLARE mycursor CURSOR FOR SELECT  fusername FROM uchome_friend;    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;    OPEN mycursor;    REPEAT         FETCH mycursor INTO a;        IF NOT done THEN            SET c=CONCAT(c,a);/*字符串相加*/        END IF;    UNTIL done END REPEAT;    CLOSE mycursor;    SELECT c;END //DELIMITER ;
0 0
原创粉丝点击