Several loop control structures in PL/SQL

Source: Internet
Author: User

1. Basic cycle
End with end loop starting with loop. To avoid endless loop, you must use the exit or exit when statement.
Create Table temp (cola int );
Declare
I INT: = 1;
Begin
Loop
Insert into temp values (I );
Exit when I = 10;
I: = I + 1;
End loop;
End;

2. While Loop

Start with while loop and end with end loop;
Declare
I INT: = 1;
Begin
While I <= 10 Loop
Insert into temp values (I );
I: = I + 1;
End loop;
End;

3. For Loop syntax
For counter in [reverse]
Lower_bound .. upper_bound Loop
Statement1;
Statement2;
..........
End loop;
By default, when the for loop is used, 1 is automatically added for each variable. If the reverse option is specified, the loop control variable automatically

Minus 1.
Begin
For I in reverse 1 .. 10 Loop
Insert into temp values (I );
End loop;
End;

4. nested loops and labels
You can use <label_name> to define labels for nested loops.

Declare
Result1 int;
Begin
<Outerr>
For I in 1 .. 100 Loop
<Inter>
For J in 1 .. 100 Loop
Result1: = I * J;
Exit outerr when result1 = 1000;
Exit when result1= 500;
End loop Inter;
Dbms_output.put_line (result1 );
End loop outerr;
Dbms_output.put_line (result1 );
End;

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.