Mysql creates a stored procedure and uses Cursor to cyclically update _ MySQL

Source: Internet
Author: User
Mysql creates a stored procedure and uses Cursor to cyclically update bitsCN.com

Use cursor)

1. declare a cursor

DECLARECursor_nameCursor for select_statement
This statement declares a cursor. You can also define multiple cursors in a subroutine, but each cursor in a block must have a unique name. A single operation is performed after the cursor is declared, but the SELECT statement cannot have an INTO clause.

2. cursor OPEN statement

OPENCursor_name
This statement opens the previously declared cursor.

3. cursor FETCH statement

FETCHCursor_nameINTO var_name [, var_name]...
This statement reads the next row with the specified open cursor (if there is a next row) and advances the cursor pointer.

4. cursor CLOSE statement

CLOSE cursor_name
This statement closes the previously opened cursor.

I checked a lot of information before writing, but I couldn't execute it all the time. I thought about it and wrote an example. it was completely correct after testing.

The following example shows how to use a cursor to update a stored procedure:

-- Procedure "useCursor" ddlcreate procedure 'usecursor '() BEGIN/* definition of local variables declare */declare aid bigint default 0; declare mdsl bigint default 0; declare stop int default 0; declare cur cursor for (select count (area_info.id) as mdsl, area_info.id as aid from area_info right join subbranch_info on region = area_info.idwhere area_info. I _record_status = 0 and region _ Status = 0 group by area_info.id);/* mysql does not know why to add an exception to the judgment? * This captures a cursor exception * and sets the loop to jump out of the loop when the variable stop is null. */Declare continue handler for sqlstate '000000' SET stop = null;/* OPEN cursor */OPEN cur;/* Move the cursor down, pay the two queried values to the two defined variables */FETCH cur INTO mdsl and aid;/* loop body, which obviously adds and uses the names queried by the cursor; no. */WHILE (stop is not null) DOupdate zlgsydrsp. area_info set I _subbran_count = mdsl where id = aid;/* Move the cursor one step down */FETCH cur INTO mdsl, aid; END WHILE; CLOSE cur; END;
BitsCN.com

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.