//
you're reading...
Database, IT-Center, Oracle

Loop And Recordset Oracle/PLSQL

The syntax for the FOR Loop is:

FOR loop_counter IN [REVERSE] lowest_number..highest_number
LOOP
     {.statements.}
END LOOP;

You would use a FOR Loop when you want to execute the loop body a fixed number of times.

Let’s take a look at an example.

FOR Lcntr IN 1..20
LOOP
     LCalc := Lcntr * 31;
END LOOP;

This example will loop 20 times. The counter will start at 1 and end at 20.

The FOR Loop can also loop in reverse. For example:

FOR Lcntr IN REVERSE 1..15
LOOP
     LCalc := Lcntr * 31;
END LOOP;

This example will loop 15 times. The counter will start at 15 and end at 1. (loops backwards)

Cursor for loop

for rec in (select col_1, col_2 from table_a) loop
  /*Statements, use rec.col_1 and rec.col_2 */
end loop;
for rec in cursor_name loop
  /*Statements, use rec.col_1 and rec.col_2 */
end loop;
for rec in cursor_name(cursor_param_1, cursor_param_2...) loop
  /*Statements, use rec.col_1 and rec.col_2 */
end loop;

 

Labels

Each of the loops can be labeled:
<<label_name>>
loop
  ....
end loop label_name;
When a loop is labeled, the exit statement can then refer to that label:
begin
  <<i_loop>> for i in 1 .. 10 loop
    <<j_loop>> for j in 1 .. 10 loop
      dbms_output.put(to_char(j, '999'));
      exit j_loop when j=i;
    end loop;
    dbms_output.new_line;
  end loop;
end;
/

About berbagisolusi

Berbagi merupakan sebuah bentuk simbol keikhlasan untuk membantu dan menolong, sedangkan solusi adalah cara menyelesaikan masalah. Setiap manusia pasti mengalami masalah, tetapi kita tidak perlu mengalami masalah yang sama jika orang lain pernah mengalami dan kita tahu hal tersebut.

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 9 other followers

December 2011
M T W T F S S
« Nov   Jan »
 1234
567891011
12131415161718
19202122232425
262728293031  

Archives

Web Statistic

Blog Stats

  • 149,580 hits
%d bloggers like this: