Loops in Oracle

Source: Internet
Author: User

 

There are five main types of loops: exit when, loop, while, for (Common loop), and for (cursor loop). The following is an example to illustrate (all stored procedures ).

1. Exit when loop:
Create or replace procedure proc_test_exit_when is
I number;
Begin
I: = 0;
Loop
Exit when (I> 5 );
Dbms_output.put_line (I );
I: = I + 1;
End loop;
End proc_test_exit_when;
------------------- The splitting line ----------------------

2. loop:
Create or replace procedure proc_test_loop is
I number;
Begin
I: = 0;
Loop
I: = I + 1;
Dbms_output.put_line (I );
If I> 5 then
Exit;
End if;
End loop;
End proc_test_loop;
------------------- The splitting line ----------------------

3. While loop:
Create or replace procedure proc_test_while is
I number;
Begin
I: = 0;
While I <5 Loop
I: = I + 1;
Dbms_output.put_line (I );
End loop;
End proc_test_while;
------------------- The splitting line ----------------------

4. For general loop:
Create or replace procedure proc_test_for is
I number;
Begin
I: = 0;
For I in 1 .. 5 Loop
Dbms_output.put_line (I );
End loop;
End proc_test_for;
------------------- The splitting line ----------------------

5. For cursor loop:
Create or replace procedure proc_test_cursor is
Userrow test % rowtype;
Cursor userrows is
Select * from test;
Begin
For userrow in userrows Loop
Dbms_output.put_line (userrow. ID | ',' | userrow. Name | ',' | userrows % rowcount );
End loop;
End proc_test_cursor;
------------------- The splitting line ----------------------

As shown above, the stored procedure correspondsCodeYou can perform the test as follows:

Go to PL/SQL, run the file-> New->ProgramWindow-> blank, copy the above sections of code, to the PL/SQL blank window, andf8 to execute the compilation.

Then execute the file-> New-> command window to enter the command window to execute the set serveroutput on code, and then enter the corresponding exec stored procedure, OK.

In the "5th" cycle, you must create a table field named "test" and insert several pieces of data to the table for testing.

This article is from the "gjhgkh" blog, please be sure to keep this source http://77857.blog.51cto.com/67857/419594

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.