Anda di halaman 1dari 7

academy.oracle.

com

Database Programming with PL/SQL Nama : Baihaqi Suhaeri


Npm : 1402018017
5-1: Introduction to Explicit Cursors Kelas : C
Tugas 5
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.
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 Select Statement that returns only one row.

3. List two circumstances in which you would use an explicit cursor.


multiple return rows ;each row needs to be fetched manually

4. Exercise using CURRENCIES tables:

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.

B. Add a statement to open the currencies_cur cursor.


C. Add variable declarations and an executable statement to read ONE row through the
currencies_cur cursor into local variables.

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 wf_currencies;
v_code wf_currencies.currency_code%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.
DECLARE
CURSOR wf_currencies_cur IS
SELECT currency_code, currency_name FROM wf_currencies;
v_code wf_currencies.currency_code%TYPE;
v_name wf_currencies.currency_name%TYPE;
BEGIN
OPEN wf_currencies_cur;
LOOP
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.
FETCH wf_currencies_cur INTO v_code, v_name;
DBMS_OUTPUT.PUT_LINE(v_code || v_name);
EXIT WHEN wf_currencies_cur%NOTFOUND;
END LOOP;
CLOSE wf_currencies_cur;
END;

AED Emirati dirham


AFA Afghani
ALL Lek
AMD Dram
ANG Netherlands Antillean guilder
AOA Kwanza
ARS Argentine peso
AUD Australian dollar
AWG Aruban guilder
AZM Azerbaijani Manat
BAM Marka
BBD Barbadian dollar
BDT Taka
BGL Lev
BIF Burundi franc
BMD Bermudian dollar
BND Bruneian dollar
BOB Boliviano
BRL Real
BSD Bahamian dollar
BTN Ngultrum
BWP Pula
BYB Belarusian ruble
BZD Belizean dollar
CAD Canadian dollar
CDF Congolese franc
CHF Swiss franc
CLP Chilean peso
CNY yuan
COP Colombian peso
CRC Costa Rican colon
CUC Convertible peso
CUP Cuban peso
CVE Cape Verdean escudo
CYP Cypriot pound
CZK Czech koruna
DJF Djiboutian franc
DKK Danish krone
DOP Dominican peso
DZD Algerian dinar
EEK Estonian kroon
EGP Egyptian pound
ERN nakfa
ETB birr
EUR Euro
FJD Fijian dollar
FKP Falkland pound
GBP British pound
GEL lari
GHC cedi
GIP Gibraltar pound
GMD dalasi
GNF Guinean franc
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.
GTQ quetzal
GYD Guyanese dollar
HKD Hong Kong dollar
HNL lempira
HRK kuna
HTG gourde
HUF forint
IDR Indonesian rupiah
ILS new Israeli shekel
INR Indian rupee
IRR Iranian rial
ISK Icelandic krona
JMD Jamaican dollar
JOD Jordanian dollar
JPY yen
KD Kuwaiti dinar
KES Kenyan shilling
KHR riel
KMF Comoran franc
KPW North Korean won
KRW South Korean won
KYD Caymanian dollar
KZT tenge
LAK kip
LBP Lebanese pound
LKR Sri Lankan rupee
LRD Liberian dollar
LSL loti
LTL litas
LVL Latvian lat
LYD Libyan dinar
MAD Moroccan dirham
MDL Moldovan leu
MGA Madagascar ariary
MKD Macedonian denar
MMK Kyat
MNT togrog/tugrik
MOP pataca
MRO ouguiya
MTL Maltese lira
MUR Mauritian rupee
MVR rufiyaa
MWK Malawian kwacha
MXN Mexican peso
MYR ringgit
MZM metical
NAD Namibian dollar
NGN naira
NID New Iraqi dinar
NOK Norwegian krone
NONE No currency used
NPR Nepalese rupee
NZD New Zealand dollar
PAB balboa
PEN nuevo sol
PGK kina
PKR Pakistani rupee
PLN zloty
PYG guarani
QAR Qatari rial
RMB Renminbi
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.
RUR Russian ruble
SAR Saudi riyal
SAT tala
SBD Solomon Islands dollar
SCR Seychelles rupee
SEK Swedish krona
SGD Singapore dollar
SHP Saint Helenian pound
SIT tolar
SLL leone
SOS Somali shilling
SRD Surinam dollar
STD dobra
SZL lilangeni
THB baht
TMM Turkmen manat
TND Tunisian dinar
TTD Trinidad and Tobago dollar
TWD new Taiwan dollar
UAH hryvnia
UGX Ugandan shilling
USD US Dollar
UYU Uruguayan peso
UZS Uzbekistani soum
VEB bolivar
VND dong
VUV vatu
XAF Communaute Financiere Africaine franc
XCD East Caribbean dollar
XOF Communaute Financiere Africaine franc
XPF Comptoirs Francais du Pacifique franc
YER Yemeni rial
YTL Turkish lira
ZAR South African rand
ZMK Zambian kwacha
ZWD Zimbabwean dollar
NIO gold cordoba
OMR Omani rial
PHP Philippine peso
RON leu
RWF Rwandan franc
SDD Sudanese dinar
SKK Slovak koruna
SYP Syrian pound
TOP paanga
TZS Tanzanian shilling
TZS Tanzanian shilling

Statement processed.

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 IS
SELECT country_name, national_holiday_date, national_holiday_name FROM
wf_countries where region_id=5;
v_country_name wf_countries.country_name%TYPE ;
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.
v_holiday wf_countries.national_holiday_date%TYPE;
v_hname wf_countries.national_holiday_name%TYPE;
BEGIN
OPEN wf_holiday_cursor ;
LOOP
FETCH wf_holiday_cursor INTO v_country_name, v_holiday, v_hname;
EXIT WHEN wf_holiday_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_country_name||' '||v_holiday||' '||v_hname);
END LOOP;
CLOSE wf_holiday_cursor;
END;
South Georgia and the South Sandwich Islands
Argentine Republic 25-May Revolution Day
Republic of Bolivia 6-Aug Independence Day
Federative Republic of Brazil 7-Sep Independence Day
Falkland Islands 14-Jun Liberation Day
Co-operative Republic of Guyana 23-Feb Republic Day
Republic of Peru 28-Jul Independence Day
Republic of Paraguay 15-May Independence Day
Republic of Ecuador 10-Aug Independence Day
Oriental Republic of Uruguay 25-Aug Independence Day
Bolivarian Republic of Venezuela 5-Jul Independence Day
Republic of Chile 18-Sep Independence Day
Republic of Colombia 20-Jul Independence Day
Republic of Suriname 25-Nov Independence Day
Department of Guiana 14-Jul Bastille Day

Statement processed.

5. Identify three guidelines for declaring and using explicit cursors.


- INTO is not used in the declarative section
-% NOTFOUND for testing the output condition
- after a cursor has been closed, it can only be called if it is reopened

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);
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.
EXIT WHEN region_cursor%NOTFOUND;
END LOOP;
CLOSE region_cursor;
END;

5 -> 15
9 -> 28
11 -> 21
18 -> 17
29 -> 25
35 -> 16
39 -> 16
145 -> 16
151 -> 13
155 -> 14
155 -> 14

Statement processed.

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.

Anda mungkin juga menyukai