PL / SQL游标计算项目数

时间:2011-12-14 16:20:27

标签: plsql cursor

我是pl / sql的新手

我必须完成一个程序,该程序计算通过程序参数解析的精选员工ID的平均值,并计算该员工已经采取的项目数量

这是我的表:

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));

这里基本上是pl / sql程序

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);

我期待 c3 必须计算员工服务项目数量的问题

计数不正确 这是我得到的输出:

Employee: 101
Employee Name: Marlen
Number of projects: 20
Average Working Hours: 107.4

感谢您的帮助!如何检查平均工作时间是否小于10,那么员工的工资保持不变,否则检查项目数是否小于4然后是工资的5%,否则工资的10%加到工资中

1 个答案:

答案 0 :(得分:0)

尝试删除group by光标中的c3子句。此外,c1是所有员工的平均工作时间:这是您所期望的吗?

为什么选择sproc?您可以使用标准聚合函数在常规查询中实现您正在执行的操作。