簡體   English   中英

PL/SQL 異常未捕獲異常

[英]PL/SQL Exception not catching the exception

你們能幫我理解為什么我的代碼不處理異常而是給出錯誤。

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

該錯誤是我放在異常塊中的東西,但仍然沒有被捕獲。

您似乎正在嘗試將日志消息插入到只接受數字的表列中。

exact fetch returns more than requested number of rows被捕獲的異常,但是在處理原始異常時引發了另一個異常,因此它們似乎同時引發。

你沒有給出你的messages表的定義,但如果我使用create table messages (results number);創建它,我能夠重現你的錯誤create table messages (results number); .

您將需要 (a) 使用類似的方法將results列的類型更改為VARCHAR2(4000)

alter table messages modify results varchar2(4000);

或 (b) 在此表中使用不同的列。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM