MySQL cursor syntax instance

Source: Internet
Author: User
Welcome to the Linux community forum and interact with 2 million technical staff. 1. Basic Syntax: SQL code # define the cursor, valuefromsys_sequence; # Use the cursor openfetchSeqCursor; # fetch data fetchcursorinto_seqname, _ value;

Welcome to the Linux community forum and interact with 2 million technical staff> 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;

Welcome to the Linux community forum and interact with 2 million technicians>

1. Basic Syntax:

SQL code

# Define a cursor

Declare fetchSeqCursor cursor for select seqname, value from sys_sequence;

# Use a cursor

Open fetchSeqCursor;

# Fetch data

Fetch cursor into _ seqname, _ value;

# Closing a cursor

Close fetchSeqCursor;

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;

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 fetchSeqOk = false;

Open fetchSeqCursor;

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;

# Nested cursor loop ends

End 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.