Specific usage of MySQL cursors

Source: Internet
Author: User

The following articles mainly describe the specific usage of MySQL cursors. If you are interested in the actual operations of MySQL cursors, you can click and view the following articles, it is hoped that some help will be provided to you after your browsing.

Use MySQL 1.5 or above;

Test Table level;

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

Insert some data;

Code

Initialization

Drop procedure if exists useCursor //

Create a stored procedure

Create procedure useCursor ()

BEGIN

Declare

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

MySQL does not know why to add an exception to the judgment?

Please refer to the official documentation for more information.

This captures MySQL cursor exceptions

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

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

Open cursor

OPEN cur1;

Move the cursor down

 
 
  1. FETCH cur1 INTO tmpName; 

Loop body, which obviously adds names queried by MySQL cursors and separates them with; numbers.

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

Move the cursor down

 
 
  1. FETCH cur1 INTO tmpName;  
  2. END WHILE;  
  3. CLOSE cur1;  
  4. select allName ;  
  5. END;//  
  6. call useCursor()//  

Running result:

Code

 
 
  1. MySQL> call useCursor()//  
  2. +--------------------------------------+  
  3. | allName |  
  4. +--------------------------------------+  
  5. | f1;c3;c6;c5;c2;c4;c1;f1;f3;f4;f2;f5; |  
  6. +--------------------------------------+  
  7. 1 row in set (0.00 sec)  

Code

 
 
  1. DELIMITER $$   
  2. DROP PROCEDURE IF EXITS cursor_example$$   
  3. CREATE PROCEDURE cursor_example()   
  4. READS SQL DATA   
  5. BEGIN   
  6. DECLARE l_employee_id INT;   
  7. DECLARE l_salary NUMERIC(8,2);   
  8. DECLARE l_department_id INT;   
  9. DECLARE done INT DEFAULT 0;   
  10. DECLARE cur1 CURSOR FOR SELECT employee_id, salary, department_id FROM employees;   
  11. DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;   
  12. OPEN cur1;   
  13. emp_loop: LOOP   
  14. FETCH cur1 INTO l_employee_id, l_salary, l_department_id;   
  15. IF done=1 THEN   
  16. LEAVE emp_loop;   
  17. END IF;   
  18. END LOOP emp_loop;   
  19. CLOSE cur1;   
  20. END$$   
  21. DELIMITER ;   

Code

Creation process

 
 
  1. DELIMITER //  
  2. DROP PROCEDURE IF EXISTS test //  
  3. CREATE PROCEDURE test()  
  4. BEGIN  
  5. DECLARE done INT DEFAULT 0;  
  6. DECLARE a VARCHAR(200) DEFAULT '';  
  7. DECLARE c VARCHAR(200) DEFAULT '';  
  8. DECLARE mycursor CURSOR FOR SELECT fusername FROM uchome_friend;  
  9. DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;  
  10. OPEN mycursor;  
  11. REPEAT   
  12. FETCH mycursor INTO a;  
  13. IF NOT done THEN  
  14. SET c=CONCAT(c,a); 

String Addition

 
 
  1. END IF;  
  2. UNTIL done END REPEAT;  
  3. CLOSE mycursor;  
  4. SELECT c;  
  5. END //  
  6. DELIMITER ; 

The above content is an introduction to the usage of MySQL cursors. I hope you will gain some benefits.

Related Article

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.