【问题标题】:PL/SQL - Why ORA-06550 can't be trapped in an exception?PL/SQL - 为什么 ORA-06550 不能被困在异常中?
【发布时间】:2012-11-13 03:07:59
【问题描述】:

我创建了一个 plsql 程序来从替换变量中获取一个employee_id。每当我尝试输入字母时,我都会收到一条错误消息,其中 ORA-06550 作为错误号。我把它放在异常部分,但它似乎没有被提出。

这是我在替换变量中输入“kk”时的错误消息...

Error report:
ORA-06550: line 13, column 9:
PLS-00201: identifier 'KK' must be declared
ORA-06550: line 13, column 1:
PL/SQL: Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

这是我的 pl/sql 块

set serveroutput on
set verify off
declare
  cursor cur(mid employees.employee_id%type) is
  select e.last_name employee, 
         m.last_name manager 
    from employees e 
    join employees m 
      on m.employee_id = e.manager_id
   where m.employee_id=mid;

   rec cur%rowtype;
   m_id employees.employee_id%type;
   ex exception;
   pragma exception_init(ex, -06550);

begin
  m_id := &id;
  open cur(m_id);
  fetch cur into rec;
    dbms_output.put_line('here '||rec.employee || ' ' || rec.manager);
  close cur;

exception
  when ex then dbms_output.put_line('employee_id was not a valid number');
end;
/

有谁知道为什么我不能捕获该异常?

【问题讨论】:

    标签: sql oracle exception plsql


    【解决方案1】:

    您无法在运行时捕获该异常,因为这是编译时错误。

    m_id := &id;

    尝试输入'KK' 而不是KK,否则将被解释为标识符。

    【讨论】:

    • 是的,它来自替换变量。它有效,谢谢。这是否意味着如果没有“”,我就不能捕获错误?
    【解决方案2】:

    我希望我能发表评论。你不能把你的代码改成:

    m_id := '&id';
    

    因此您不需要捕获该异常。

    捕获号码异常的更新:

    declare
        (...)
    begin
        m_id := to_number('&id');
        (...)
    exception
        when VALUE_ERROR then
             dbms_output.put_line('You provided invalid number'); 
    end;
    

    【讨论】:

    • 是的,我可以这样做,但我需要用户输入一个数字,如果用户输入一个字母,那是唯一一次异常会捕获它们,我从中遇到了一些问题。 :)
    【解决方案3】:

    您的代码绝对正确。请使用员工 ID 而不是表中的 KK。它必须是一个数字。例如 101 或 102

    在游标的 where 子句中使用“m.employee_id=mid”,且employee_id 的数据类型为数字。

    谢谢:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-04
      • 2012-04-13
      • 1970-01-01
      • 1970-01-01
      • 2011-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多