PLSQL 4 3 Practice

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

www.oracle.

com/academy

Database Programming with PL/SQL

4-3: Iterative Control: Basic Loops


Practice Activities
Vocabulary
Identify the vocabulary word for each definition below:

Loop basico Encloses a sequence of statements between the keywords LOOP and
END LOOP and must execute at least once.

Exit Statement to terminate a loop.

Try It / Solve It
1. What purpose does a loop serve in PL/SQL?
ayuda a poder realizar varias situaciones.

2. List the types of loops in PL/SQL.


Loop basico, FOR, WHILE.

3. What statement is used to explicitly end a loop?


Exit
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_nombre_pais varchar2(50);
begin
loop select country_id, country_name into v_id, v_nombre_pais
from countries
where v_id=country_id;
dbms_output.put_line('Id del pais '||v_id||'. el nombre es: '||v_nombre_pais);
v_id:=v_id+1;
if v_id>3 then exit;
end if;
end loop;
end;

Copyright © 2018, 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.
2

5. Modify your solution to question 4 above, replacing the IF statement with an EXIT....WHEN
statement.
declare
v_id number:=1;
v_nombre_pais varchar2(50);
begin
loop select country_id, country_name into v_id, v_nombre_pais
from countries
where v_id=country_id;
dbms_output.put_line('Id del pais '||v_id||'. el nombre es: '||v_nombre_pais);
v_id:=v_id+1;
exit when v_id>3;
end loop;
end;

6. Create a MESSAGES table and insert several rows into it.

A. To create the messages table.

DROP TABLE messages;


CREATE TABLE messages (results NUMBER(2));

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_numero number(2):=1;
begin
loop if v_numero<>6 AND v_numero<>8 then
insert into messages(results)values(v_numero);
end if;
v_numero:=v_numero+1;
exit when v_numero>10;
end loop;
end;
C. Execute a SELECT statement to verify that your PL/SQL block worked.
select * from messages

Copyright © 2018, 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.

You might also like