There are five main loops: Exit when, loop, while, for (normal loop), for (cursor loop), and below for example one by one (all stored procedures). 1, Exit when cycle: 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; —————————————————— – I'm a split-line —————————————————————
2. Loop 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; —————————————————— – I'm a split-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; —————————————————— – I'm a split-line ————————————————————— 4. For general circulation: 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; —————————————————— – I'm a split-line ————————————————————— 5. For cursor loops: 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; —————————————————— – I'm a split-line ————————————————————— As the corresponding code for the stored procedure is shown above, you can test it in the following ways: Enter Pl/sql, execute file-> new-> program window-> blank, copy the above paragraph code, to Pl/sql blank window, Ann F8 perform compilation. Re-execute File-> the new-> Command window goes to the command window to execute the set serveroutput on this code, and then enter the exec corresponding stored procedure, OK. The 5th loop requires that you create a new table field ID named Test, name, and insert a few data to test. |