第1步:找出你想要捕获哪些错误:
如果表格不存在:
SQL> drop table x;
drop table x
*
ERROR at line 1:
ORA-00942: table or view does not exist
如果表中使用:
SQL> create global temporary table t (data varchar2(4000));
Table created.
使用在另一个会话表。 (注意没有插入后提交或任何东西。)
SQL> insert into t values ('whatever');
1 row created.
早在第一届会议,试图删除:
SQL> drop table t;
drop table t
*
ERROR at line 1:
ORA-14452: attempt to create, alter or drop an index on temporary table already in use
所以两个错误陷阱:
- ORA- 00942:表或视图不存在
- ORA-14452:尝试 在已使用的临时表上创建,更改或删除索引
看错误是否为predefined。他们不是。因此,他们需要像这样定义:
create or replace procedure p as
table_or_view_not_exist exception;
pragma exception_init(table_or_view_not_exist, -942);
attempted_ddl_on_in_use_GTT exception;
pragma exception_init(attempted_ddl_on_in_use_GTT, -14452);
begin
execute immediate 'drop table t';
exception
when table_or_view_not_exist then
dbms_output.put_line('Table t did not exist at time of drop. Continuing....');
when attempted_ddl_on_in_use_GTT then
dbms_output.put_line('Help!!!! Someone is keeping from doing my job!');
dbms_output.put_line('Please rescue me');
raise;
end p;
和结果,第一不t
:
SQL> drop table t;
Table dropped.
SQL> exec p;
Table t did not exist at time of drop. Continuing....
PL/SQL procedure successfully completed.
而现在,t
使用:
SQL> create global temporary table t (data varchar2(4000));
Table created.
在另一个会话:
SQL> insert into t values (null);
1 row created.
然后在第一个会话中:
SQL> exec p;
Help!!!! Someone is keeping from doing my job!
Please rescue me
BEGIN p; END;
*
ERROR at line 1:
ORA-14452: attempt to create, alter or drop an index on temporary table already in use
ORA-06512: at "SCHEMA_NAME.P", line 16
ORA-06512: at line 1
该表是否真的是全局临时表? ('创建全局临时表....')如果是这样,为什么你放弃它?这是安装脚本的一部分吗?如果没有,也许全球临时表可以满足您的需求,而无需丢弃它。 –
好吧,我们遇到了“已经存在”的问题,不知何故,它没有从productino环境中得到证实,表格的状态如何。此表不是安装脚本的一部分,而是其单独过程的一部分。 –
我不明白,你为什么遇到一个全球临时表已经存在的问题。该表应该已经存在,代码只是使用(插入,删除,更新等)它。 –