0

嗨,我是 pl/sql 的新手

我需要将所有员工少于 5 人的项目的 rate_per_hour 更新为 500

这是我写的代码,但它更新了所有员工

set serveroutput on

declare
  cursor rate_cur is
  select * from project
  for update of rate_per_hour;
begin
  for rate_rec IN rate_cur
  loop
    update project
    set rate_per_hour=500
    where current of rate_cur;
  end loop;

end;

这是我的桌子:

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




  Please suggest a solution
4

2 回答 2

1

它会全部更新,因为您正在无限制地选择所有项目。您可以将光标更改为:

declare
  cursor RATE_CUR is
    select     P.PROJECTNO, count(*) as EMP_CNT
    from       PROJECT P
               join ASSIGNMENT A
                 on A.PROJECTID = P.PROJECTNO
    group by   PROJECTNO
    having     count(*) < 5;
begin
  for RATE_REC in RATE_CUR loop
    update PROJECT
    set    RATE_PER_HOUR = 500
    where  PROJECTNO = RATE_REC.PROJECTNO;
  end loop;
end;
于 2011-12-15T04:03:13.553 回答
1

我在另一个论坛上找到了帮助!这是代码

update project
set    rate_per_hour = 500
where  projectno IN 
    (select projectid
     from assignment
     group by projectid
     having count(distinct empid) <5);
于 2011-12-15T15:37:09.943 回答