0% found this document useful (0 votes)
116 views

PLSQL 5 Practice

The document provides examples of PL/SQL code using cursors to retrieve and display data from tables in an Oracle database. It includes examples that declare and use explicit cursors to fetch rows one at a time or in a loop. The examples demonstrate using cursors with record structures, ordering results, and exiting loops based on row counts or end of data conditions.

Uploaded by

Purv Sutariya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views

PLSQL 5 Practice

The document provides examples of PL/SQL code using cursors to retrieve and display data from tables in an Oracle database. It includes examples that declare and use explicit cursors to fetch rows one at a time or in a loop. The examples demonstrate using cursors with record structures, ordering results, and exiting loops based on row counts or end of data conditions.

Uploaded by

Purv Sutariya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

N01580206 Yug Sutariya

1. 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.

Ans :- DECLARE
CURSOR cur_curr IS
SELECT currency_code,currency_name
FROM currencies
ORDER BY currency_name ASC;
v_currency_name currencies.currency_name%TYPE;
v_currency_code currencies.currency_code%TYPE;
B. Add a statement to open the currencies_cur cursor.

Ans :- OPEN cur_curr;

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

Ans :- FETCH cur_curr INTO v_currency_name,v_currency_code;

D. Add a statement to display the fetched row, and a statement to close the currencies_cur cursor.
Ans :- DBMS_OUTPUT.PUT_LINE(v_currency_name||''||v_currency_code);

CLOSE cur_curr;

E. Run your block to confirm that it works. It should display: AFA Afghani.
Ans :-
N01580206 Yug Sutariya

academy.oracle.com

Database Programming with PL/SQL

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.

Ans :-

DECLARE

CURSOR cur_curr IS

SELECT *

FROM currencies

ORDER BY currency_name ASC;

v_cur_curr cur_curr%ROWTYPE;
N01580206 Yug Sutariya

BEGIN

OPEN cur_curr;

LOOP

FETCH cur_curr INTO v_cur_curr;

DBMS_OUTPUT.PUT_LINE(v_cur_curr.currency_code||' '||v_cur_curr.currency_name||' '||


v_cur_curr.comments);

EXIT WHEN cur_curr%NOTFOUND;

END LOOP;

CLOSE cur_curr;

END;
N01580206 Yug Sutariya

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).
Ans :-
DECLARE
CURSOR country IS
SELECT country_name,national_holiday_date,national_holiday_name FROM countries
WHERE region_id=5 AND national_holiday_date IS NOT NULL;
v_country_name countries.country_name%TYPE;
v_national_holiday_date countries.national_holiday_date%TYPE;
v_national_holiday_name countries.national_holiday_name%TYPE;
BEGIN
OPEN country;
LOOP
FETCH country INTO v_country_name,v_national_holiday_date,v_national_holiday_name;
N01580206 Yug Sutariya

DBMS_OUTPUT.PUT_LINE(v_country_name||' '||v_national_holiday_date||' '||


v_national_holiday_name);
EXIT WHEN country%NOTFOUND;
END LOOP;
END;
N01580206 Yug Sutariya

2. 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.

Ans :- DECLARE
CURSOR curr IS
SELECT r.region_name , COUNT(c.country_id)
FROM regions r, countries c
WHERE r.region_id=c.region_id
GROUP BY r.region_name
HAVING COUNT(c.country_id)>10
ORDER BY r.region_name ASC;
v_region_name regions.region_name%TYPE;
v_country_id countries.country_id%TYPE;
BEGIN
OPEN curr;
LOOP
N01580206 Yug Sutariya

FETCH curr INTO v_region_name,v_country_id;


DBMS_OUTPUT.PUT_LINE(v_region_name||' '||v_country_id);
EXIT WHEN curr%NOTFOUND;
END LOOP;
CLOSE curr;
END;
N01580206 Yug Sutariya

3. Write a PL/SQL block to read through 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. Use a record structure to hold all the columns
selected from the countries table.
Ans:-

DECLARE

CURSOR country IS

SELECT country_name,national_holiday_date,national_holiday_name FROM countries

WHERE region_id=5 AND national_holiday_date IS NOT NULL;

v_count_na country%ROWTYPE;

BEGIN
N01580206 Yug Sutariya

OPEN country;

LOOP

FETCH country INTO v_count_na;

DBMS_OUTPUT.PUT_LINE(v_count_na.country_name||' '||v_count_na.national_holiday_date||'
'||v_count_na.national_holiday_name);

EXIT WHEN country%NOTFOUND;

END LOOP;

END;
N01580206 Yug Sutariya

4. For this exercise, you use the employees table. Create a PL/SQL block that fetches and
displays the six employees with the highest salary. For each of these employees, display the first
name, last name, job id, and salary. Order your output so that the employee with the highest salary
is displayed first. Use %ROWTYPE and the explicit cursor attribute %ROWCOUNT.
Ans :-
DECLARE
CURSOR sal IS
SELECT first_name,last_name,job_id,salary
FROM employees
ORDER BY salary DESC;
v_sal sal%ROWTYPE;
BEGIN
OPEN sal;
LOOP
FETCH sal INTO v_sal;
DBMS_OUTPUT.PUT_LINE(v_sal.first_name||' '||v_sal.last_name||' '||v_sal.job_id||' '||
v_sal.salary);
EXIT WHEN sal%ROWCOUNT=6 OR sal%NOTFOUND;
END LOOP;
CLOSE sal;
END;
N01580206 Yug Sutariya

5. Look again at the block you created in question 3. What if you wanted to display 21 employees
instead of 6? There are only 20 rows in the employees table. What do you think would happen?
N01580206 Yug Sutariya

Ans :- Here , if I give condition rowcount=40 which is maximum number of rows then display buffer
overflow error and warning so raise application error.

6. . In real life we would not know how many rows the table contained. Modify your block from
question 3 so that it will exit from the loop when either 21 rows have been fetched and displayed,
or when there are no more rows to fetch. Test the block again.
Ans :-
DECLARE
CURSOR sal IS
SELECT DISTINCT first_name,last_name,job_id,salary
FROM employees
ORDER BY salary DESC;
v_sal sal%ROWTYPE;
v_counter INTEGER;
BEGIN
OPEN sal;
LOOP
FETCH sal INTO v_sal;
DBMS_OUTPUT.PUT_LINE(sal%ROWCOUNT||' '||v_sal.first_name||' '||v_sal.last_name||' '||
v_sal.job_id||' '||v_sal.salary);
EXIT WHEN sal%ROWCOUNT=21 OR sal%NOTFOUND;
END LOOP;
CLOSE sal;
END;
N01580206 Yug Sutariya

7. Using the COUNTRIES table, write a cursor that returns countries with a highest_elevation
greater than 8,000 m. For each country, display the country_name, highest_elevation, and climate.
N01580206 Yug Sutariya

Use a cursor FOR loop, declaring the cursor using a subquery in the FOR…LOOP statement. 5.
This question uses a join of the SPOKEN_LANGUAGES and COUNTRIES tables with a GROUP
BY and HAVING clause.
Ans:-
DECLARE
BEGIN
FOR v_rec IN (SELECT c.country_name,c.highest_elevation,c.climate
FROM countries c , spoken_languages s
WHERE c.country_id=s.country_id
GROUP BY c.country_name,c.highest_elevation,c.climate
HAVING c.highest_elevation>8000)
LOOP
DBMS_OUTPUT.PUT_LINE('Country Name :'||' '||v_rec.country_name||' '||' Highest
elevation :'||' '||v_rec.highest_elevation||' '||'Climate :'||' '||v_rec.climate);
END LOOP;
END;

8. Write a PL/SQL block to fetch and display all the countries that have more than six spoken
languages. For each such country, display country_name and the number of spoken languages.
N01580206 Yug Sutariya

Use a cursor FOR loop, but declare the cursor explicitly in the DECLARE section. After all the rows
have been fetched and displayed, display an extra row showing the total number of countries
having more than six languages. (Hint: Declare a variable to hold the value of %ROWCOUNT.)
Ans :-
DECLARE
v_lan INTEGER;
CURSOR curr IS
SELECT c.country_name ,COUNT(s.language_id) AS CO
FROM spoken_languages s , countries c
WHERE c.country_id=s.country_id
GROUP BY c.country_name
HAVING COUNT(s.language_id)>6 ;
v_cal INTEGER;
BEGIN
FOR v_rec IN curr LOOP
DBMS_OUTPUT.PUT_LINE(v_rec.country_name||' '||v_rec.co);
v_cal:= curr%ROWCOUNT;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Total record are'||' '||v_cal);
END;
N01580206 Yug Sutariya
N01580206 Yug Sutariya

9. Write a PL/SQL block to display the country name and the area of each country in a chosen
region. The region_id should be passed to the cursor as a parameter. Test your block using two
region_ids: 5 (South America) and 30 (Eastern Asia). Do not use a cursor FOR loop.
Ans :-
Region_id:- 5
DECLARE
CURSOR country(v_reg NUMBER) IS
SELECT country_name,area
FROM countries c
WHERE region_id=v_reg;
v_record country%ROWTYPE;
BEGIN
OPEN country(5);
LOOP
FETCH country INTO v_record;
DBMS_OUTPUT.PUT_LINE(v_record.country_name||' '||v_record.area);
EXIT WHEN country%NOTFOUND;
END LOOP;
CLOSE country;
END;
N01580206 Yug Sutariya

Region_id :- 30
DECLARE
N01580206 Yug Sutariya

CURSOR country(v_reg NUMBER) IS


SELECT country_name,area
FROM countries c
WHERE region_id=v_reg;
v_record country%ROWTYPE;
BEGIN
OPEN country(30);
LOOP
FETCH country INTO v_record;
DBMS_OUTPUT.PUT_LINE(v_record.country_name||' '||v_record.area);
EXIT WHEN country%NOTFOUND;
END LOOP;
CLOSE country;
END;
N01580206 Yug Sutariya

10. Modify your answer to question 2 to use a cursor FOR loop. You must still declare the cursor
explicitly in the DECLARE section. Test it again using regions 5 and 30.
Ans :-
Region_id:-5

DECLARE
CURSOR country IS
SELECT country_name,national_holiday_date,national_holiday_name FROM countries
WHERE region_id=5 AND national_holiday_date IS NOT NULL;
v_count_na country%ROWTYPE;

BEGIN

FOR v_count_na IN country LOOP


DBMS_OUTPUT.PUT_LINE(v_count_na.country_name||' '||
v_count_na.national_holiday_date||' '||v_count_na.national_holiday_name);
EXIT WHEN country%NOTFOUND;
END LOOP;
END;
N01580206 Yug Sutariya

Region_id=30:-

DECLARE
CURSOR country IS
SELECT country_name,national_holiday_date,national_holiday_name FROM countries
WHERE region_id=30 AND national_holiday_date IS NOT NULL;
v_count_na country%ROWTYPE;

BEGIN

FOR v_count_na IN country LOOP


DBMS_OUTPUT.PUT_LINE(v_count_na.country_name||' '||
v_count_na.national_holiday_date||' '||v_count_na.national_holiday_name);
EXIT WHEN country%NOTFOUND;
END LOOP;
END;
N01580206 Yug Sutariya

You might also like