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