Explicit Cursors Cursors Close Context Area Implicit Cursor Open Fetch Active Set
Explicit Cursors Cursors Close Context Area Implicit Cursor Open Fetch Active Set
Explicit Cursors Cursors Close Context Area Implicit Cursor Open Fetch Active Set
Explicit Cursors Declared by the programmer for queries that return more than one
row
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
Statement that retrieves the current row and advances the cursor to
fetch 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
2
Try It / Solve It
1. In your own words, explain the difference between implicit and explicit cursors.
An implicit is created by the database when certain events happen, whilst a explicit cursor
is user defined and can hold multiple rows.
2. Which SQL statement can use either an explicit or an implicit cursor, as needed?
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.
open currencies_cur;
C. Add variable declarations and an executable statement to read ONE row through the
currencies_cur cursor into local variables.
v_curr_code currencies.currency_code%TYPE;
v_curr_name currencies.currency_name%TYPE;
D. Add a statement to display the fetched row, and a statement to close the
currencies_cur cursor.
DECLARE
CURSOR wf_currencies_cur IS
SELECT currency_code, currency_name FROM wf_currencies
BEGIN
OPEN wf_currencies_cur;
FETCH wf_currencies_cur INTO v_curr_code, v_curr_name;
DBMS_OUTPUT.PUT_LINE(v_curr_code || ' is ' || v_curr_name);
CLOSE wf_currencies_cur;
END;
4
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.
DECLARE
CURSOR wf_currencies_cur IS
SELECT currency_code, currency_name FROM wf_currencies
v_curr_code wf_currencies.currency_code%TYPE;
v_curr_name wf_currencies.currency_name%TYPE;
BEGIN
OPEN wf_currencies_cur;
LOOP
END LOOP;
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_cur IS
SELECT country_name, national_holiday_date, national_holiday_name FROM wf_countries
OPEN wf_holiday_cur;
LOOP
v_national_holiday_name;
END LOOP;
CLOSE wf_holiday_cur;
END;
5
5. Identify three guidelines for declaring and using explicit cursors.
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.
DECLARE
CURSOR region_cursor IS
SELECT region_id, COUNT(*) AS how_many FROM
wf_countries
GROUP BY region_id HAVING
COUNT(*) > 10;
v_reg wf_countries.region_id%TYPE; nr
PLS_INTEGER;
BEGIN
OPEN region_cursor;
LOOP
FETCH region_cursor INTO v_reg, nr;
DBMS_OUTPUT.PUT_LINE(v_reg||' -> '||nr);
EXIT WHEN region_cursor%NOTFOUND;
END LOOP;
CLOSE region_cursor;
END;