------example 1: loops with IF statement-----------
set serveroutput on
declare
v_counter binary_integer := 0;
begin
loop
--increment loop counter by one
v_counter := v_counter + 1;
DBMS_OUTPUT.put_line('v_counter = '||v_counter);
--if exit condition yields true exit the loop
if v_counter = 5 then -- the same as : exit when v_counter = 5;
exit;
end if;
end loop;
--control resumes here
dbms_output.put_line('Done...');
end;
------example 2: loops with EXIT WHEN conditions-----------
set serveroutput on
declare
v_course course.courser_no%type :430;
v_instructor_id instructor.instructor_id%type :102;
v_sec_num section.section_no%type :=0;
begin
loop
--increment section number by one
v_sec_num := v_sec_num + 1;
insert into section
(section_no,course_no,section_no,instructor_id,
created_date, created_by,modified_date,
modified_by)
values
(section_id_seq.nextval,v_course,v_sec_num,
v_instructor_id,SYSDATE,USER,sysdate,user);
--if number of section added is four exit the loop
exit when v_sec_num = 4;
end loop;
--control resumes here
commit;
end;
--------exiample 3: loops with WHILE Loops condition---------
set serveroutput on
declare
c_counter binary_integer := 1;
v_sum number :=0;
begin
while v_counter <= 10 loop
v_sum := v_sum + v_counter;
dbms_output.put_line('current sum is: '||v_sum);
-- increment loop counter by one
v_counter := v_counter + 1;
end loop;
-- control resumes here
dbms_output.put_line('the sum of integers between 1 '||
'and 10 is: '||v_sum);
end;
---------example 4: loop with For Loops----------
set serveroutput on
declare
v_factorial number := 1;
begin
--the loop counter is defined implicitly by the loop
--Therefore,before the loop ,the loop counter is undefined and has no value;
for v_counter in 1..10 loop
v_factorial := v_factorial * v_counter;
end loop;
--countrol resume here
dbms_output.put_line('factorial of ten is :'||v_factorial);
end;
plsql学习:loop使用
最新推荐文章于 2024-08-26 20:46:11 发布