使用计数的 PL/SQL 更新率

发布于 2024-12-21 05:53:19 字数 1167 浏览 2 评论 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

Hi am newbie at pl/sql

I need to update rate_per_hour for all projects having less than 5 employee to 500

here is the code what i wrote, but it updates all employee

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;

Here is my tables:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海夜未眠 2024-12-28 05:53:19

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

update project
set    rate_per_hour = 500
where  projectno IN 
    (select projectid
     from assignment
     group by projectid
     having count(distinct empid) <5);

I found help on another forums! Here is the code

update project
set    rate_per_hour = 500
where  projectno IN 
    (select projectid
     from assignment
     group by projectid
     having count(distinct empid) <5);
醉城メ夜风 2024-12-28 05:53:19

它会更新所有内容,因为您无限制地选择所有项目。您可以将光标更改为类似以下内容:

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;

It updates all because you are selecting all projects without limit. You can change the cursor to something like:

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文