在 table 中找不到给定记录时如何处理游标中的异常

How to handle exception in cursor when given record is not found in a table

我正在使用 Cursor For 循环更新 emp3(与 emp table 相同),

DECLARE 
   CURSOR incr_cur IS SELECT * FROM emp3 FOR UPDATE OF sal;
   v_job emp3.job%TYPE := '&ENTER_Job';
   v_cnt INTEGER := 0;
BEGIN
  FOR r_l IN incr_cur LOOP
    IF v_job IN (r_l.job) THEN
      UPDATE emp3 SET sal = sal + 100 WHERE CURRENT OF incr_cur;
    END IF;
 END LOOP;

 FOR r_l IN incr_cur LOOP
    IF v_job IN (r_l.job) THEN
      v_cnt := v_cnt + 1;
      DBMS_OUTPUT.PUT_LINE('The Salary of ' || r_l.ename || ' is Incremented by 100 and the Updated Salary is: $' || r_l.sal);
    END IF;
 END LOOP;

     DBMS_OUTPUT.PUT_LINE('The Salary of '|| v_cnt ||' Employees are Updated');
END;

当执行 PL/SQL 块时,它会请求作业, 我给MANAGER,那么MANAGER的员工工资加100。 emp3 table 有 5 个工作类别 CLERKMANAGERANALYSTSALESMANPRESIDENT。 那么如何显示消息 未列出作业,因此无法更新。,如果用户输入不在 table 中的作业,例如 DEVELOPER. 我尝试过异常处理,但无法正常工作。

这里有一个选项:检查是否存在这样的工作;如果不是,查询将 return NO_DATA_FOUND 您可以处理并使用适当的消息引发异常。否则,继续 UPDATE.

SQL> declare
  2    l_job emp.job%type;
  3  begin
  4    begin
  5      select job
  6        into l_job
  7        from emp
  8        where job = '&ENTER_Job'
  9          and rownum = 1;
 10    exception
 11      when no_data_found then
 12        raise_application_error(-20000, 'That job does not exist');
 13    end;
 14
 15    -- Job exists, so - go on with the update
 16  end;
 17  /
Enter value for enter_job: MANAGER

PL/SQL procedure successfully completed.

SQL> /
Enter value for enter_job: DEVELOPER
declare
*
ERROR at line 1:
ORA-20000: That job does not exist
ORA-06512: at line 12


SQL>

P.S。忘了说:我更喜欢通过存储过程(接受作业名称作为参数)而不是匿名 PL/SQL 块来完成这样的工作。

不需要单独的步骤。只需尝试更新,如果没有更新任何行,请说明。如果你想让它成为一个例外,那么用 raise_application_error.

提出一个

假设这是一个学习练习,这就是为什么你不想只做一个普通的 update,你可以这样做:

declare 
    k_job constant emp3.job%type := '&JOB';

    cursor employees_cur is
        select * from emp3
        where  job = k_job
        for update of sal;

    v_update_count integer := 0;
    v_payroll_increase integer := 0;
begin
    for r in employees_cur loop
        update emp3 set sal = sal + 100 where current of employees_cur;
        dbms_output.put_line('Salary for ' || r.ename || ' is incremented by 0 from $' || r.sal || ' to $' || (r.sal +100));
        v_update_count := v_update_count + 1;
        v_payroll_increase := v_payroll_increase + 100;
    end loop;

    if v_update_count = 0 then
        dbms_output.put_line('No staff are currently employed as ' || k_job ||'. Payroll is unchanged.');
    else
        dbms_output.put_line('Updated salary of '|| v_update_count ||' employee' || case when v_update_count <> 1 then 's' end||'.');
        dbms_output.put_line('Payroll increased by $'||v_payroll_increase||'.');
    end if;
end;
/

Enter value for job: SALESMAN
Salary for ALLEN is incremented by 0 from 00 to 00
Salary for WARD is incremented by 0 from 50 to 50
Salary for MARTIN is incremented by 0 from 50 to 50
Salary for TURNER is incremented by 0 from 00 to 00
Updated salary of 4 employees.
Payroll increased by 0.

PL/SQL procedure successfully completed.

对于一个不存在的工作,你得到这个:

Enter value for job: ASTRONAUT
No staff are currently employed as ASTRONAUT. Payroll is unchanged.

(在此示例中,v_payroll_increase 始终是 v_update_count 的 100 倍,但如果您想加薪 10% 或按部门等进行不同的加薪,这可能更有用。)