Usage notes for MySQL cursors

Source: Internet
Author: User
Tags define local

The following articles mainly introduce the notes for using MySQL cursors, which can be used in SQL statements of stored procedures. The main types are as follows, I believe that if you master this technology, it will be of great help in your future studies or work.

1. No return result statements, such as INSERT, UPDATE, DROP, and DELETE.

2. The select statement returns a single-row variable and can be passed to the local variable (select ..)

3. Return the select statement of the multi-row result set and can be processed cyclically using the MySQL cursor.

Note: The multi-row result set returned by a stored procedure can be received by a client program such as php, but it is impossible to receive the result set of another stored procedure in one stored procedure, the general solution is to store a temporary table for sharing in other processes.

4. prepare statement

The following describes the cursor and prepare

Cursor

Definition

 
 
  1. DECLARE cursor_name CURSOR FOR SELECT_statement; 

Cursor operation

OPEN cursor

 
 
  1. OPEN cursor_name; 

FETCH: obtains the record of the current pointer of the cursor and sends it to the specified Variable list. Note that the number of variables must be the same as the number of fields returned by the MySQL cursor. To obtain multiple rows of data, run the FETCH statement in a loop.

 
 
  1. FETCH cursor_name INTO variable list; 

CLOSE cursor

 
 
  1. CLOSE cursor_name ; 

Note: The MySQL cursor is read-only, that is, you can only read the result set sequentially from the beginning to the end, not from the back to the front, or directly jump to the middle of the record.

A complete example:

Define local variables

 
 
  1. DECLARE o varchar(128); 

Define a cursor

 
 
  1. DECLARE ordernumbers CURSOR  
  2. FOR  
  3. SELECT callee_name FROM account_tbl where acct_timeduration=10800;  
  4. DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_departments=1;  
  5. SET no_more_departments=0;  

Open cursor

 
 
  1. OPEN ordernumbers; 

Loop all rows

 
 
  1. REPEAT  
  2. -- Get order number  
  3. FETCH ordernumbers INTO o;  
  4. update account set allMoneyallMoney=allMoney+72,lastMonthConsumelastMonthConsume=lastMonthConsume-72 where NumTg=@o;  

Loop ends

 
 
  1. UNTIL no_more_departments  
  2. END REPEAT; 

Close cursor

 
 
  1. CLOSE ordernumbers; 

The above content is an introduction to the notes for using MySQL cursors. I hope you will have some gains.

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.