PLSQL 6 1 Practice
PLSQL 6 1 Practice
c
om
Database Programming
with PL/SQL 6-1: Introduction
to Explicit Cursors Practice
Activities
Vocabulary
Identify the vocabulary word for each
definition below:
Explicit Cursor Declared by the programmer for queries that return more than one
row
Cursor
A label for a context area or a pointer to the context area
Close
Disables a cursor, releases the context area, and undefines the active
set
Context Area
An allocated memory area used to store the data processed by a SQL
statement
Implicit Cursor
Defined automatically by Oracle for all SQL DML statements, and for
SELECT statements that return only one row
Open Statement that executes the query associated with the cursor,
identifies the active set, and positions the cursor pointer to the first
row
Fetch Statement that retrieves the current row and advances the cursor to
the next row either until there are no more rows or until a specified
condition is met
Active Set
The set of rows returned by a multiple row query in an explicit cursor
operation
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.
2
Try It / Solve It
1. In your own words, explain the difference between implicit and explicit cursors.
Un cursor implícito se crea por la misma base de datos cuando un evento especifico sucede y uno
explicito es definido por el usuario y puede sostener multiples filas.
A. Write a PL/SQL block to declare a cursor called currencies_cur. The cursor will be used
to read and display all rows from the CURRENCIES table. You will need to retrieve
currency_code and currency_name, ordered by ascending currency_name.
D. Add a statement to display the fetched row, and a statement to close the currencies_cur cursor.
E. Run your block to confirm that it works. It should display: AFA Afghani.
DECLARE CURSOR wf_currencies_cur IS SELECT currency_code, currency.name
FROM w SELECT currency_code, currency.name FROM wf_currenc f_currencies;
v_code wf_currencies.currency_code%TYPE; v_name wf_currencies.currency.name%TYPE;
v_name wf_currencies.currency.name%TYPE;
BEGIN OPEN wf_currencies_cur;
FETCH wf_currencies_cur
INTO v_code, v_name; DBMS_OUTPUT.PUT_LINE(v_code || v_name);
CLOSE wf_currencies_cur;
END;
F. Your code so far displays only one row. Modify your code so that it fetches and displays all
the rows, using a LOOP and EXIT statement. Test your modified block. It should fetch and
display each row in the CURRENCIES table. If it doesn't, check that your EXIT statement is in
the correct place in the code.
v_code wf_currencies.currency_code%TYPE;
v_name wf_currencies.currency_name%TYPE;
BEGIN
OPEN wf_currencies_cur;
LOOP
FETCH wf_currencies_cur
DBMS_OUTPUT.PUT_LINE(v_code ||
CLOSE wf_currencies_cur;
END;
G. Write and test a PL/SQL block to read and display all the rows in the COUNTRIES table for all
countries in region 5 (South America region). For each selected country, display the
country_name, national_holiday_date, and national_holiday_name. Display only those
countries having a national holiday date that is not null. Save your code (you will need it in the
next practice).
DECLARE CURSOR wf_holiday_cursor
LOOP END;
FETCH wf_holiday_cursor
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.
6. Write a PL/SQL block to read and display the names of world regions, with a count of
the number of countries in each region. Include only those regions having at least 10
countries. Order your output by ascending region name.
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.