I am newbie at pl/sql
I have to complete procedure that computes average of select employee id that parsed through procedure paramet and calculate number of projects that that employee have taken
here is my table:
CREATE TABLE employee(
empid number(5),
empname varchar(20),
address varchar(20),
no_of_dependents number(5),
deptno number(5),
CONSTRAINT EMPLOYEE_PKEY PRIMARY KEY(empid),
CONSTRAINT EMPLOYEE_FKEY FOREIGN KEY(deptno) REFERENCES department(deptno));
CREATE TABLE project(
projectno number(5),
location varchar(20),
incharge number(5),
rate_per_hour number(5),
CONSTRAINT PROJECT_PKEY PRIMARY KEY(projectno),
CONSTRAINT PROJECT_FKEY FOREIGN KEY(incharge) REFERENCES employee(empid));
CREATE TABLE assignment(
empid number(5),
projectid number(5),
hours number(5),
CONSTRAINT ASSIGNMENT_FKEY FOREIGN KEY(empid) REFERENCES employee(empid),
CONSTRAINT ASSIGNEMNT_FKEY2 FOREIGN KEY(projectid) REFERENCES project(projectno));
and here is basically pl/sql procedure
set serveroutput on
create or replace procedure disp(idd number) is
cursor c1 is select avg(ass.hours) from assignment ass join employee e on
e.empid=ass.empid
where ass.empid=idd;
cursor c2 is select empname from employee where empid=idd;
v_name varchar(20);
cursor c3 is select count(p.projectno)
from employee e join project p on
e.empid=p.incharge
where p.incharge=idd;
v_count_nr number;
begin
open c1;
fetch c1 into v_avg_nr;
close c1;
open c2;
fetch c2 into v_name;
close c2;
open c3;
fetch c3 into v_count_nr;
close c3;
dbms_output.put_line('Employee: '||idd);
dbms_output.put_line('Employee Name: '||v_name);
dbms_output.put_line('Number of projects: '||v_count_nr);
dbms_output.put_line('Average Working Hours: '||v_avg_nr);
end disp;
/
execute disp(101);
I am expecting problems where the c3 has to count the number of projects employee took
It is counting incorrectly
here is the output what I am getting:
Employee: 101
Employee Name: Marlen
Number of projects: 20
Average Working Hours: 107.4
Thanks for help! how can I check if the average working hours is less than 10 then employee’s salary remain the same, otherwise check if number of project is less than 4 then 5% of salary, else 10% of salary is added to the salary
Try removing the
group byclause in yourc3cursor. Also,c1is the average working hours for all employees: is this what you’re expecting?Why an sproc? You can achieve what you are doing in a regular query using standard aggregation functions.