Code instance created by a stored procedure with a cursor in a MySQL database

Source: Internet
Author: User

This article mainly introducesMySQL databaseOfStored Procedure with CursorFor example, let's take a look at how to create a stored procedure with a cursor.

The code example is as follows:

 
 
  1. DELIMITER $$  
  2.  
  3. DROP PROCEDURE IF EXISTS `jf`.`insert2`$$  
  4.  
  5. CREATE PROCEDURE `insert2`(in yhbh varchar(50),out bjbh varchar(50))  
  6.  
  7. BEGIN  
  8.  
  9. declare tempbjbh varchar(50) default 0;  
  10.  
  11. declare done int default 0;  
  12.  
  13. declare cur1 CURSOR FOR select ffgl.c_bjbh from ffgl,dj_info where ffgl.c_yhbh='U01010101' and ffgl.n_fflx=dj_info.n_fflx order by ffgl.c_bjbh asc;  
  14.  
  15. declare CONTINUE HANDLER FOR SQLSTATE '02000' SET done=1;   
  16.  
  17. open cur1;  
  18.  
  19. Repeat  
  20.  
  21. fetch cur1 into tempbjbh;  
  22.  
  23. if not done then   
  24.  
  25. set tempbjbh=concat(tempbjbh,"/");  
  26.  
  27. set bjbh=concat(bjbh,tempbjbh);  
  28.  
  29. end if;  
  30.  
  31. until done end repeat;  
  32.  
  33. close cur1;  
  34.  
  35. select bjbh;   
  36.  
  37. END$$  
  38.  
  39. DELIMITER ; 

Note:The cursor definition statement must be placed behind the definition of other variables. Otherwise, an error occurs. For example, if the positions of rows 8th and 9th are changed, an error is returned.

The code example of the stored procedure with a cursor in the MySQL database has been introduced. If you want to learn more about the MySQL database, please read the article: http://database.51cto.com/mysql/. your harvest is definitely for our work!

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.