0% found this document useful (0 votes)
99 views4 pages

Tema Nr. 4: Observație!

The document contains 7 questions about PL/SQL cursors. The questions cover declaring and using explicit and implicit cursors to retrieve and display data from tables. Examples include using cursors to display country data by region, department and employee data, and modifying blocks to accept parameters and exit loop conditions.

Uploaded by

Andrei Tigan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views4 pages

Tema Nr. 4: Observație!

The document contains 7 questions about PL/SQL cursors. The questions cover declaring and using explicit and implicit cursors to retrieve and display data from tables. Examples include using cursors to display country data by region, department and employee data, and modifying blocks to accept parameters and exit loop conditions.

Uploaded by

Andrei Tigan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Tema nr.

4
Observație!
Scrieți rezolvarea direct în acest document!

1. Write and test a PL/SQL block to read and display all the rows in the wf_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. DECLARE

DECLARE
CURSOR wf_holidays IS
SELECT country_name, national_holiday_date, national_holiday_name
FROM wf_countries
WHERE region_id=5 AND national_holiday_date IS NOT NULL;
v_country_name wf_countries.country_name%TYPE;
v_national_holiday_date wf_countries.national_holiday_date%TYPE;
v_national_holiday_name wf_countries.national_holiday_name%TYPE;
BEGIN
DBMS_OUTPUT.PUT_LINE('Tarile din regiunea 5 si sarbatorile lor nationale sunt:');
OPEN wf_holidays;
LOOP
FETCH wf_holidays INTO v_country_name, v_national_holiday_date,
v_national_holiday_name;
EXIT WHEN wf_holidays%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_country_name||' '||v_national_holiday_date||'
'||v_national_holiday_name);
END LOOP;
CLOSE wf_holidays;
END;

2. For this exercise, you use the employees and departments table. Create a PL/SQL block that fetches and
displays the names of the 5 departments with the most employees (Hint: use a join condition). For each of
these departments, display the department name and the number of employees. Order your output so that the
department with the most employees is displayed first. Use %ROWTYPE and the explicit cursor attribute
%ROWCOUNT.

DECLARE
CURSOR cursor_department IS
SELECT d.department_name, count(e.employee_id)
FROM departments d, employees e
WHERE d.department_id=e.department_id
GROUP BY d.department_name
ORDER BY 2 DESC;
v_dept_name departments.department_name%TYPE;
v_nr_employees NUMBER;
BEGIN
DBMS_OUTPUT.PUT_LINE('Primele 5 departamente sunt :');
OPEN cursor_department;
LOOP
FETCH cursor_department INTO v_dept_name, v_nr_employees;
EXIT WHEN cursor_department%ROWCOUNT>5;
DBMS_OUTPUT.PUT_LINE( 'Departamentul se numeste: '|| v_dept_name|| ' iar nr. de angajati este :
'||v_nr_employees);
END LOOP;
CLOSE cursor_department;
END;

3. Look again at the block you created in question 2. What if you wanted to display 10 departments instead of
5? There are only 7 rows in the departments table. What do you think would happen?
Cred ca instructiunea LOOP va incerca sa afiseze un al 8-lea rand, care nu exista.

4. In real life we would not know how many rows the table contained. Modify your block from question 2 so
that it will exit from the loop when either 10 rows have been fetched and displayed, or when there are no
more rows to fetch. Test the block again.

DECLARE
CURSOR cursor_department IS
SELECT d.department_name, count(e.employee_id)
FROM departments d, employees e
WHERE d.department_id=e.department_id
GROUP BY d.department_name
ORDER BY 2 DESC;
v_dept_name departments.department_name%TYPE;
v_nr_employees NUMBER;
BEGIN
DBMS_OUTPUT.PUT_LINE(' Departamentele sunt :');
OPEN cursor_department;
LOOP
FETCH cursor_department INTO v_dept_name, v_nr_employees;
EXIT WHEN cursor_department%ROWCOUNT>10 OR cursor_department%NOTFOUND;
DBMS_OUTPUT.PUT_LINE( 'Departamentul se numeste: '|| v_dept_name|| ' iar nr. de angajati este :
'||v_nr_employees);
END LOOP;
CLOSE cursor_department;
END;

5. Modify the following PL/SQL block so that it uses a cursor FOR loop. Keep the explicit cursor declaration in
the DECLARE section. Test your changes.

DECLARE
CURSOR wf_currencies_cur IS
SELECT currency_code, currency_name
FROM wf_currencies
ORDER BY currency_name;
v_curr_code wf_currencies.currency_code%TYPE;
v_curr_name wf_currencies.currency_name%TYPE;
BEGIN
OPEN wf_currencies_cur;
LOOP
FETCH wf_currencies_cur
INTO v_curr_code, v_curr_name;
EXIT WHEN wf_currencies_cur%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_curr_code || ' ' || v_curr_name);
END LOOP;
CLOSE wf_currencies_cur;
END;

DECLARE
CURSOR wf_currencies_cursor IS
SELECT currency_code, currency_name
FROM wf_currencies
ORDER BY currency_name;
BEGIN
FOR v_wf_currencies_cursor IN wf_currencies_cursor
LOOP
DBMS_OUTPUT.PUT_LINE(v_wf_currencies_cursor.currency_code || ' ' ||
v_wf_currencies_cursor.currency_name);
END LOOP;
END;

6. Write a PL/SQL block to display the country_name and area of all countries 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.

DECLARE
CURSOR country_cursor
(v_region_id wf_countries.region_id%TYPE) IS
SELECT country_name, area FROM wf_countries
WHERE region_id = v_region_id;
country_record country_cursor%ROWTYPE;
BEGIN
OPEN country_cursor(5) ;
LOOP
FETCH country_cursor INTO country_record;
EXIT WHEN country_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Name: ' || country_record.country_name ||
' Area: ' || country_record.area);
END LOOP;
CLOSE country_cursor;
END;

7. Write and run a PL/SQL block which produces a listing of departments and their employees. Use the
departments and employees tables. In a cursor FOR loop, retrieve and display the department_id and
department_name for each department, and display a second line containing ‘----------‘ as a separator. In a
nested cursor FOR loop, retrieve and display the first_name, last_name and salary of each employee in that
department., followed by a blank line at the end of each department. Order the departments by
department_id, and the employees in each department by last_name.
You will need to declare two cursors, one to fetch and display the departments, the second to fetch and
display the employees in that department, passing the department_id as a parameter.

Your output should look something like this (only the first few departments are shown):
10 Administration
-----------------------------
Jennifer Whalen 4400

20 Marketing
-----------------------------
Pat Fay 6000
Michael Hartstein 13000

50 Shipping
-----------------------------
Curtis Davies 3400
Randall Matos 2600
Kevin Mourgos 5800
Trenna Rajs 3500
Peter Vargas 2500

DECLARE
CURSOR dept_cursor IS
SELECT department_id,department_name
FROM departments
ORDER BY department_name;
CURSOR emp_cursor(v_deptno departments.department_id%TYPE) IS
SELECT first_name, last_name, salary
FROM employees
WHERE department_id = v_deptno
ORDER BY last_name;
BEGIN
FOR dept_record IN dept_cursor
LOOP
DBMS_OUTPUT.PUT_LINE(dept_record.department_id ||
' ' || dept_record.department_name);
DBMS_OUTPUT.PUT_LINE('-----------------------------');
FOR emp_record IN emp_cursor(dept_record.department_id)
LOOP
DBMS_OUTPUT.PUT_LINE (emp_record.first_name || ' ' ||
emp_record.last_name || ' ' ||
emp_record.salary);
END LOOP;
END LOOP;
END;

You might also like