MySQL cursor operation guide

Source: Internet
Author: User

MySQL cursor operation guide
This article provides a detailed analysis of the usage of MySQL cursors. For more information, see

Test Table level

The Code is as follows:

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

Insert some data;

Initialization

The Code is as follows:

drop procedure if exists useCursor //

The create code for creating a stored procedure is as follows:
CREATE PROCEDURE useCursor() BEGIN

The declare code for defining local variables is as follows:
declare tmpName varchar(20) default '' ;  declare allName varchar(255) default '' ;  declare cur1 CURSOR FOR SELECT name FROM test.level ; 

Capture when a MySQL cursor is abnormal

 

And set the loop to jump out of the loop with the variable tmpname null.

The Code is as follows:

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

  

The code for opening a cursor is as follows:
OPEN cur1;

  

The cursor goes down with the following code:
FETCH cur1 INTO tmpName;

  

Loop body, which obviously adds the names queried by the MySQL cursor and uses them; Code to separate the numbers is as follows:
WHILE ( tmpname is not null) DO set tmpName = CONCAT(tmpName ,";") ; set allName = CONCAT(allName ,tmpName) ;

  

The cursor goes down with the following code:
FETCH cur1 INTO tmpName;

  

End loop body: the code is as follows:
END WHILE;

  

The code for disabling a cursor is as follows:
CLOSE cur1;

  

The data selection code is as follows:
select allName ;

  

The code for ending the stored procedure is as follows:
END;//

  

Call the stored procedure: the code is as follows:
call useCursor()//
 
Loop cursor: the code is as follows:
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 loop cursor: the code is as follows:
/* Creation process */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; if not done then set c = CONCAT (c, a);/* string addition */end if; UNTIL done end repeat; CLOSE mycursor; SELECT c; END // DELIMITER;

  

The Code is as follows:


/* Creation process */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; if not done then set c = CONCAT (c, a);/* string addition */end if; UNTIL done end repeat; CLOSE mycursor; SELECT c; END // DELIMITER;

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.