PLSQL 4 3 Practice
PLSQL 4 3 Practice
PLSQL 4 3 Practice
com
Basic Loop Encloses a sequence of statements between the keywords LOOP and
END LOOP and must execute at least once.
Try It / Solve It
1. What purpose does a loop serve in PL/SQL?
Membantu untuk melakukan perulangan bebrapa kondisi.
4. Write a PL/SQL block to display the country_id and country_name values from the COUNTRIES
table for country_id whose values range from 1 through 3. Use a basic loop. Increment a variable
from 1 through 3. Use an IF statement to test your variable and EXIT the loop after you have
displayed the first 3 countries.
declare
v_id number:=1;
v_country_name varchar2(50);
begin
loop select country_id, country_name into v_id, v_country_name
from countries
where v_id=country_id;
dbms_output.put_line('Country Id '||v_id||'. Name: '|| v_country_name);
v_id:=v_id+1;
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
if v_id>3 then exit;
end if;
end loop;
end;
5. Modify your solution to question 4 above, replacing the IF statement with an EXIT ... WHEN
statement.
declare
v_id number:=1;
v_country_name varchar2(50);
begin
loop select country_id, country_name into v_id, v_country_name
from countries
where v_id=country_id;
dbms_output.put_line('Country Id '||v_id||'. Country Name: '||v_country_name);
v_id:=v_id+1;
exit when v_id>3;
end loop;
end;
B. Write a PL/SQL block to insert numbers into the MESSAGES table. Insert the numbers 1
through 10, excluding 6 and 8.
declare
v_number number(2):=1;
begin
loop if v_number<>6 AND v_number<>8 then
insert into messages(results)values(v_number);
end if;
v_number:=v_number+1;
exit when v_number>10;
end loop;
end;
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.