Oracle 错误处理

发布于 2024-10-03 04:42:35 字数 495 浏览 3 评论 0原文

我有这样的代码:

DECLARE
  e_not_exist EXCEPTION;
  PRAGMA EXCEPTION_INIT(e_not_exist, -942);
  car_name VARCHAR2(20);
BEGIN
  select name_of_factory into car_name from car where car_id = 1;
  dbms_output.put_line(car_name);
EXCEPTION
  when e_not_exist then
    dbms_output.put_line('Table or view does not exist');
  when OTHERS then
    dbms_output.put_line(to_char(SQLCODE));
END;

实际上,我的表名称是 CARS 而不是 CAR。但是oracle不处理这个异常,并给我一个错误ORA-00942:表或视图不存在。 我该如何处理这个异常?

I have such code:

DECLARE
  e_not_exist EXCEPTION;
  PRAGMA EXCEPTION_INIT(e_not_exist, -942);
  car_name VARCHAR2(20);
BEGIN
  select name_of_factory into car_name from car where car_id = 1;
  dbms_output.put_line(car_name);
EXCEPTION
  when e_not_exist then
    dbms_output.put_line('Table or view does not exist');
  when OTHERS then
    dbms_output.put_line(to_char(SQLCODE));
END;

Actually, my table name is CARS but not CAR. But oracle doesn't handle this exception and gives me an error ORA-00942: Table or view doesn't exist.
How can I handle this exception?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

虐人心 2024-10-10 04:42:35

ORA-00942 错误通常是编译时错误。 Oracle 必须在编译时解析表的名称。异常处理程序将在运行时而不是编译时捕获错误。

如果您使用动态 SQL,则可以将名称解析推迟到运行时,此时您可以捕获异常,即,

SQL> ed
Wrote file afiedt.buf

  1  declare
  2    no_such_table exception;
  3    pragma exception_init( no_such_table, -942 );
  4    l_cnt integer;
  5  begin
  6    execute immediate 'select count(*) from emps' into l_cnt;
  7  exception
  8    when no_such_table
  9    then
 10      dbms_output.put_line( 'No such table' );
 11* end;
SQL> /
No such table

PL/SQL procedure successfully completed.

但这通常不是编写存储过程的明智方法。您的过程应该知道实际存在哪些表,并且应该在开发期间而不是在运行时识别和解决语法错误。

An ORA-00942 error is generally going to be a compile time error. Oracle has to resolve the name(s) of the tables at compile time. Exception handlers will trap errors at runtime, not compile time.

If you used dynamic SQL, you can postpone the resolution of names to runtime at which point you can catch the exception, i.e.

SQL> ed
Wrote file afiedt.buf

  1  declare
  2    no_such_table exception;
  3    pragma exception_init( no_such_table, -942 );
  4    l_cnt integer;
  5  begin
  6    execute immediate 'select count(*) from emps' into l_cnt;
  7  exception
  8    when no_such_table
  9    then
 10      dbms_output.put_line( 'No such table' );
 11* end;
SQL> /
No such table

PL/SQL procedure successfully completed.

But that is not a sensible way, in general, to write stored procedures. Your procedures should know what tables actually exist and syntax errors should be identified and resolved during development, not at runtime.

南渊 2024-10-10 04:42:35

静态 SQL 无法做到这一点。错误是在编译代码而不是执行代码时出现的。试试这个:

 execute immediate 'select name_of_factory from car where car_id = 1' 
                    into car_name ;

You can't do that with static SQL. The error is coming when the code is being compiled, not executed. Try this instead:

 execute immediate 'select name_of_factory from car where car_id = 1' 
                    into car_name ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文