MySQL cursor syntax instance

Source: Internet
Author: User


MySQL cursor syntax Example 1. Basic Syntax: SQL code # define the cursor declare fetchSeqCursor cursor for select seqname, value from sys_sequence; # Use the cursor open fetchSeqCursor; # fetch Data fetch cursor into _ seqname, _ value; # close the cursor close fetchSeqCursor; www.2cto.com 2. Single-cursor example: SQL code # define the flag for loop judgement declare fetchSeqOk boolean; # define the varient for store the data declare _ seqname varchar (50); declare _ value bigint (20); # define the cursor declare fetchSeqCursor cursor for select seqname, value from sys_sequence; ## define the continue handler for not found flag declare continue handler for not found set fetchSeqOk = true; set fetchSeqOk = false; open fetchSeqCursor; fetchSeqLoop: loop if fetchSeqOk then leave fetchSeqLoop; else fetch cursor into _ seqname, _ value; select _ seqname, _ value; end if; end Loop; close fetchSeqCursor; www.2cto.com 3. nested cursor loop Java code # define the flag for loop judgement declare fetchSeqOk boolean; # define the varient for store the data declare _ seqname varchar (50 ); declare _ value bigint (20); # define the cursor declare fetchSeqCursor cursor for select seqname, value from sys_sequence; # define the continue handler for not found flag declare continue handler for not found set fetchSeqOk = true; set lifecycle = false; keep open Loop; fetchSeqLoop: Loop if fetchSeqOk then leave fetchSeqLoop; else fetch cursor into _ seqname, _ value; # nested cursor loop begin declare fetchSeqOk boolean default 'inner '; ## define the cursor declare cursor2 cursor for select .... from ...; # define the continue handler for not declare continue handler for not found set fetchSeqOk = true; set fetchSeqOk = false; open cursor2; fetchloop2 loop if fetchSeqOk then else end if; end loop; close cursor2; end; # end of nested cursor Loop if; end Loop; close fetchSeqCursor;

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.