PLSQL提取不会退出

时间:2015-11-19 00:28:45

标签: oracle plsql

拥有此员工表: enter image description here

和plsql查询:

DECLARE
    totaal_salaris    INT := 0;
    CURSOR medewerker_cur IS
      SELECT naam, maandsal
      FROM MEDEWERKERS;
      medewerker_row    medewerker_cur%rowtype;
BEGIN  
    OPEN medewerker_cur;
    LOOP
      FETCH medewerker_cur INTO medewerker_row;
      EXIT WHEN medewerker_cur%NOTFOUND OR totaal_salaris > 50000;
      UPDATE medewerkers
        SET maandsal = (medewerker_row.maandsal * 1.10)
        WHERE naam = medewerker_row.naam;
      totaal_salaris := totaal_salaris + medewerker_row.maandsal;
    END LOOP;
    dbms_output.put_line('Totaal uit te keren salaris: ' || totaal_salaris);
END;
/

目的是将所有现有员工的薪水(maandsal)增加10%,只要总薪水<1%。 50.000。但这根本没有用。

我仔细检查了运行脚本的女巫,这是我得到的结果:

某些测试结果:

首次投放:

PL / SQL程序已成功完成.Tital uit te keren salaris:55000

maandsal变成了5500;

第二次

PL / SQL程序已成功完成.Tital uit te keren salaris:55000

mandsal改为6050

重生时间

PL / SQL过程成功完成。 Totaal uit te keren salaris:54450

mandsal改为6655

1 个答案:

答案 0 :(得分:1)

这应该是解决方案。

DECLARE
    total_salaris    INT := 0;

    CURSOR employee_cur IS
      SELECT employee_id, first_name, salary
      FROM employees;

    employee_rec   employee_cur%rowtype;

BEGIN  
    OPEN employee_cur;
    LOOP
      FETCH employee_cur INTO employee_rec;
      total_salaris := total_salaris + (employee_rec.salary * 1.1);
      EXIT WHEN employee_cur%NOTFOUND OR total_salaris > 50000;

      UPDATE employees
        SET salary = (employee_rec.salary * 1.10)
        WHERE employee_id = employee_rec.employee_id;

    END LOOP;
    CLOSE employee_cur;

END;