PL / SQL异常未捕获到异常

时间:2018-11-04 07:41:44

标签: oracle plsql

你们能帮助我理解为什么我的代码不处理异常而是给出错误的原因吗?

SQL> SET SERVEROUTPUT ON

DEFINE p_salary

DECLARE
v_fname employees.first_name%TYPE;
v_lname employees.last_name%TYPE;
v_salary employees.salary%TYPE := &p_salary;

BEGIN
SELECT first_name, last_name
INTO v_fname, v_lname
FROM employees
WHERE salary = v_salary;

INSERT INTO messages(results)
VALUES(v_fname || ' ' 
               || v_lname 
               || ' is the only employee with the salary of '
               || v_salary);

EXCEPTION
WHEN TOO_MANY_ROWS THEN
    INSERT INTO messages(results)
    VALUES('More than one employee with a salary of ' || v_salary);

WHEN NO_DATA_FOUND THEN
    INSERT INTO messages(results) 
    VALUES('No employee with the salary of ' || v_salary);

WHEN OTHERS THEN
    INSERT INTO messages(results)
    VALUES('Some other error occurred.');

END;
/SQL> SQL> SP2-0135: symbol p_salary is UNDEFINED
SQL> SQL>   2    3    4    5    6    7    8    9   10   11   12   13   14   
15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   
31   32  
Enter value for p_salary: 6000
old   4:     v_salary employees.salary%TYPE := &p_salary;
new   4:     v_salary employees.salary%TYPE := 6000;
DECLARE
*
ERROR at line 1:
ORA-01722: invalid number
ORA-06512: at line 20
ORA-01422: exact fetch returns more than requested number of rows

该错误是我放入异常块中的东西,但仍然无法捕获。

1 个答案:

答案 0 :(得分:0)

似乎您正在尝试将日志记录消息插入仅接受数字的表的列中。

exact fetch returns more than requested number of rows异常已被捕获,但是在处理原始异常时又引发了另一个异常,因此似乎它们都被引发了。

您没有提供messages表的定义,但是如果我使用create table messages (results number);创建了错误,则可以重现您的错误。

您将需要(a)使用类似的方式将results列的类型更改为VARCHAR2(4000)

alter table messages modify results varchar2(4000);

或(b)在此表中使用其他列。