Rakha Haris TI3B
Rakha Haris TI3B
Rakha Haris TI3B
TI – 3B
DECLARE
CURSOR currencies_cur IS
SELECT currency_code,currency_name FROM currencies ORDER BY
CURRENCY_NAME;
v_curr_code currencies.currency_code%TYPE;
v_curr_name currencies.currency_name%TYPE;
BEGIN
OPEN currencies_cur;
FETCH currencies_cur INTO v_curr_code, v_curr_name;
DBMS_OUTPUT.PUT_LINE(v_curr_code|| ' ' || v_curr_name);
CLOSE 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 currencies_cur IS
SELECT currency_code,currency_name FROM currencies ORDER BY
CURRENCY_NAME;
v_curr_code currencies.currency_code%TYPE;
v_curr_name currencies.currency_name%TYPE;
BEGIN
OPEN currencies_cur;
LOOP
FETCH currencies_cur INTO v_curr_code, v_curr_name;
DBMS_OUTPUT.PUT_LINE(v_curr_code|| ' ' || v_curr_name);
EXIT WHEN currencies_cur%NOTFOUND;
END LOOP;
CLOSE 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.
DECLARE
v_c_name countries.country_name%TYPE;
v_h_date countries.national_holiday_date%TYPE;
v_h_name countries.national_holiday_name%TYPE;
CURSOR countries_cur IS
SELECT country_name, national_holiday_date, national_holiday_name
FROM countries WHERE region_id = 5 AND national_holiday_date IS NOT NULL;
BEGIN
OPEN countries_cur;
LOOP
FETCH countries_cur INTO v_c_name, v_h_date, v_h_name;
EXIT WHEN countries_cur%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('country: ' || v_c_name ||
' National Holiday: ' || v_h_name || ' Held on: ' || v_h_date);
END LOOP
CLOSE;
END;
2. 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.
SET SERVEROUTPUT ON;
DECLARE
CURSOR countries_cur IS
SELECT country_name, national_holiday_name, national_holiday_date
FROM countries WHERE region_id = 5;
countries_rec countries_cur%ROWTYPE;
BEGIN
OPEN countries_cur;
LOOP
FETCH countries_cur INTO countries_rec;
EXIT WHEN countries_cur%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Country : '||countries_rec.country_name||', National
Holiday :
'||countries_rec.national_holiday_name||
', held on : '||countries_rec.national_holiday_date);
END LOOP;
CLOSE countries_cur;
END;
3. 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.
BEGIN
FOR countries_rec IN
(SELECT country_name, highest_elevation, climate
FROM countries
WHERE highest_elevation > 8000)
LOOP
DBMS_OUTPUT.PUT_LINE('Country name: '|| countries_rec.country_name ||'the
highest elevation is:
'||
countries_rec.highest_elevation||' and the climate is: '||countries_rec.climate);
END LOOP;
END;
Procedure and Function
1. What is the difference between the following two pieces of code?
BEGIN
pay_raise;
END;
6. Create, save, and execute a procedure which updates the salary of employees in
employees_dup according to the following rules:
- if the employee is in department 80, the new salary = 1000
- if the employee is in department 50, the new salary = 2000
- if the employee is in any other department, the new salary = 3000.
You will need to include three UPDATE statements, one for each of the above rules. In a later
lesson you will learn how to avoid this. Execute your procedure from an anonymous block and
verify that the updates have been performed correctly.
BEGIN
salaryChange;
END;
7. Using the COUNTRIES table:
a. Create a procedure that accepts a country_id as a parameter and displays the name of
the country and its capitol city. Name your procedure get_country_info. Save your
procedure definition for later use.
8. Create a function called full_name. Pass two parameters to the function, an employee’s last
name and first name. The function should return the full name in the format, last name, comma,
space, first name (for example: Smith, Joe).
SET SERVEROUTPUT ON;
CREATE OR REPLACE FUNCTION full_name
(p_last_name employees.last_name%TYPE, p_first_name employees.first_name%TYPE)
RETURN VARCHAR2
IS
BEGIN
RETURN (p_last_name||', '||p_first_name);
END;
9. Test your function from an anonymous block which uses a local variable to store and display
the returned value.
DECLARE
v_full_name VARCHAR2(50);
BEGIN
v_full_name := full_name('Smith','Joe');
DBMS_OUTPUT.PUT_LINE('The name is: '|| v_full_name);
END;
10. Create a function called divide that accepts two numbers as input and returns the result of
dividing the first number by the second number, rounded to two decimal places. Save your code.
SET SERVEROUTPUT ON;
CREATE OR REPLACE FUNCTION divide(n1 NUMBER, n2 NUMBER)
RETURN NUMBER IS
rez NUMBER;
BEGIN
rez:=ROUND(n1/n2, 2);
RETURN rez;
END;
11. Test your function twice from an anonymous block using input values (50, 2) and (25, 3).
DECLARE
v_test NUMBER;
BEGIN
v_test:=divide(50,2);
DBMS_OUTPUT.PUT_LINE(v_test);
END;
DECLARE
v_test NUMBER;
BEGIN
v_test:=divide(25,3);
DBMS_OUTPUT.PUT_LINE(v_test);
END;