Lab Plsql3
Lab Plsql3
Functions
Remember to: SET SERVEROUTPUT ON. Also, the code
examples below use the LECT tables. So, if you have
used the My_ prefix on your tables, use them.
Task 1: a first cursor
The code below is designed to print out the salaries over 30000 for all personnel. You
can type this code in directly if you wish or copy and paste it. What we want you to do is
look at the output you get and be able to describe what each line of code is doing.
DECLARE
CURSOR high_salaries IS SELECT snum, surname,salary FROM personnel
where salary > 30000;
v_snum
personnel.snum%TYPE;
v_surname
personnel.surname%TYPE;
v_salary
personnel.salary%TYPE;
BEGIN
OPEN high_salaries;
LOOP
FETCH high_salaries INTO v_snum,v_surname,v_salary;
EXIT when high_salaries%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_snum ||' '||v_surname||' '|| v_salary);
END LOOP;
CLOSE high_salaries;
END;
Write your brief notes about the code here:
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
24/12/2015
Page 1
Run it.
24/12/2015
Page 2
Do the CREATE PROCEDURE process again, adding the WHERE clause as shown
above.
You will probably get the message
" ERROR at line 1: ORA-00955: name is already used by an existing object"
Why?
..
To solve this problem, you could DROP the procedure first. But there is a better way:
Use
CREATE OR REPLACE PROCEDURE .
at the top and try again.
That should have worked.
Note: The words OR REPLACE after CREATE make sure that if the procedure exists
already, you can edit it.
24/12/2015
Page 3
end loop;
end main_proc;
Task 7: Function
Study the function and procedure below. They are written to calculate the total mileage
for a given division name.
The procedure accepts a division name and then calls a function to calculate the total
mileage, which then returns the result to the procedure. The result is printed to the
screen.
Your task is to ANNOTATE the code with comments so that you understand what the
code is doing.
CREATE OR REPLACE PROCEDURE avemiles(divisionName varchar2)
is
V_divID
Branch.div%type;
V_aveMiles
personnel.salary%type;
BEGIN
SELECT div into v_divID
FROM branch WHERE divname=divisionName;
v_aveMiles:=get_aveMiles(v_divID);
DBMS_OUTPUT.PUT_LINE('Division '||v_divID||' has '||v_aveMiles||' average mileage');
END;
24/12/2015
Page 4
Execute avemiles(ADMIN)
So now you can play around with the code by trying it out on different Division Names to
see if it works.
What would you change if you wanted to calculate the maximum mileage for any
division?
24/12/2015
Page 5
function multiply
return number
is
begin
return v_1*v_2;
end multiply;
function divide
return number
is
begin
return v_1/v_2;
end divide;
begin
if op='+' then v_return:=Add;
elsif op='-' then v_return:=subtract;
elsif op='*' then v_return:=multiply;
elsif op ='/' then v_return:=divide;
else v_return:=null;
end if;
return v_return;
end calculate;
To test the program you can use Oracles DUAL dummy table in SELECT statements.
For instance, try:
SELECT calculate(2,+,3) from dual;
This should call the function and perform the arithmetic for you. Make sure you
understand how the calling is achieved in the above statement.
Try passing various parameters to test the code works. Try passing a zero for a division
for example!
24/12/2015
Page 6
end if;
return v_return;
end calc_stats;
Remember to test the code by typing SELECT calc_stats(average) from dual;
24/12/2015
Page 7