PLSQL 4 3 Practice

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

academy.oracle.

com

Database Programming with PL/SQL


4-3: Iterative Control: Basic Loops
Practice Activities
Vocabulary
Identify the vocabulary word for each definition below:

Basic Loop 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?
Membantu untuk melakukan perulangan bebrapa kondisi.

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


Basic Loop, For dan 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_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;

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_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;

C. Execute a SELECT statement to verify that your PL/SQL block worked.


select * from messages

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy