Oracle 错误处理
我有这样的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ORA-00942 错误通常是编译时错误。 Oracle 必须在编译时解析表的名称。异常处理程序将在运行时而不是编译时捕获错误。
如果您使用动态 SQL,则可以将名称解析推迟到运行时,此时您可以捕获异常,即,
但这通常不是编写存储过程的明智方法。您的过程应该知道实际存在哪些表,并且应该在开发期间而不是在运行时识别和解决语法错误。
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.
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.
静态 SQL 无法做到这一点。错误是在编译代码而不是执行代码时出现的。试试这个:
You can't do that with static SQL. The error is coming when the code is being compiled, not executed. Try this instead: