Mysql usage of those things (14) cursor

Source: Internet
Author: User


Mysql (14) usage of the cursor links: Mysql (1) Installation of mysql http://www.bkjia.com/database/201210/162314.html Mysql (2) Database Operations http://www.bkjia.com/database/201210/162315.html Mysql (3) operations on Data Tables http://www.bkjia.com/database/201210/162316.html Mysql (4) Data Table query operations http://www.bkjia.com/database/201210/162317.html Mysql (5) operation time http://www.bkjia.com/database/201210/162318.html;Mysql Those things (6) String Pattern Matching http://www.bkjia.com/database/201210/163969.html;Mysql Those things (7) In-depth select query http://www.bkjia.com/database/201210/163970.html;Mysql Those things (8) Index http://www.bkjia.com/database/201210/163971.html Mysql (9) common functions http://www.bkjia.com/database/201210/164229.html Mysql (10) trigger 1 http://www.bkjia.com/database/201210/164516.html Mysql (11) trigger 2 http://www.bkjia.com/database/201210/164766.html Mysql stored procedures http://www.bkjia.com/database/201210/164795.html Mysql (13) usage of variables and conditions http://www.bkjia.com/database/201211/165662.html In the stored procedure, you can use the cursor to process the result set cyclically. The use of the cursor includes the declaration, open, fetch, and close of the cursor. The syntax of www.2cto.com is as follows: SQL code -- DECLARE the CURSOR DECLARE cur_name CURSOR FOR select_statement -- open CURSOR OPEN cursor_name -- FETCH CURSOR FETCH cursor_name INTO var_name [, var_name...] -- close cursor CLOSE cursor_name example: SQL code delimiter $ -- change; terminator to $ -- create procedure payment_amount () BEGIN --- DECLARE variable DECLARE I _staff_id int; DECLARE d_amount decimal (5, 2); --- DECLARE a cursor and obtain the value of staff_id and amount columns in the table payment. DECLARE cur_payment cursor for select staff_id, amount from payment; --- condition processing. The condition for judging the end of a loop is to capture the not found condition. --- When fecth cannot find the next record, it will close the cursor and exit the process. Declare exit handler for not found close cur_payment; set @ x1 = 0; set @ x2 = 0; ---- OPEN the cursor OPEN cur_payment; repeat fetch cur_payment INTO I _staff_id, d_amount; if I _staff_id = 2 then set @ x1 = @ x1 + d_amount; else set @ x2 = @ x2 + d_amount; end if; UNTIL 0 END REPEAT; CLOSE cur_payment; END; $ delimiter; DECLARE are defined in sequence: variables and conditions must be placed before the cursor declaration, and finally the handler declaration.

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.