删除全局临时表

16

两个独立的问题。

  1. I am using this script to drop a table [SOLVED]

    BEGIN
        EXECUTE IMMEDIATE 'DROP TABLE_NAME';
        DBMS_OUTPUT.PUT_LINE ('Global table TABLE_NAME Dropped');
        EXCEPTION
            WHEN OTHERS THEN
                DBMS_OUTPUT.PUT_LINE ('Global table TABLE_NAME Doesn''t exist.');
    END;
    /
    

有没有办法区分表格是“不存在”还是正在其他会话中使用(如果是这种情况,它将被锁定并且无法删除)。我不确定是否可以在 user_tables 中看到该表格是否存在。我对权限不太了解。

我现在已添加了此代码

WHEN OTHERS THEN
        i_code  :=  SQLCODE;
        v_errm  :=  SUBSTR(SQLERRM, 1, 64);
  if i_code = -942 THEN
    DBMS_OUTPUT.PUT_LINE ('TABLE_NAME doesn''t exist. Script will continue to create it');
  ELSE
    DBMS_OUTPUT.PUT_LINE ('Error dropping temporary table. The error code is ' || i_code || '- ' || v_errm);
  END IF ;

2. 我看到每个过程的结尾都有“.”,就像这样

END PROCEDURE_NAME;
.
/
sho err;

我不明白为什么.在这里。它是语法吗?这与IT技术有关。

这个表真的是全局临时表吗?(create global temporary table ....) 如果是,为什么要删除它?这是安装脚本的一部分吗?如果不是,也许一个全局临时表可以满足您的需求,而无需删除它。 - Shannon Severance
我们遇到了“已存在”的问题,但不知道表的状态是否在生产环境中被确认。这个表不是安装脚本的一部分,而是一个独立的过程。 - x.509
我不明白,为什么你会遇到一个全局临时表已经存在的问题。这个表应该已经存在了,代码只是使用它(插入、删除、更新等)。 - Shannon Severance
@Nader的答案对我有效。 - yu yang Jian
5个回答

26
-- 首先清空临时表
SQL> TRUNCATE TABLE test_temp1;

-- 然后删除临时表
SQL> DROP TABLE test_temp1;

在重新连接(更改会话)后,它在我的情况下起作用了。 - daniel.kahlenberg
这是最佳答案! - yu yang Jian

16
步骤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

所以要捕获的两个错误是:

  1. ORA-00942:表或视图不存在
  2. ORA-14452:尝试在已使用的临时表上创建、修改或删除索引

查看这些错误是否预定义。但事实上它们不是。因此需要像下面这样定义:

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

-1

这就是问题所在。这里(http://download.oracle.com/docs/cd/B10501_01/appdev.920/a96624/07_errs.htm)可能会有其他的异常情况。 - x.509

-1

DECLARE GLOBAL TEMPORARY TABLE语句为当前连接定义了一个临时表。

这些表不驻留在系统目录中,也不是持久的。

临时表仅存在于声明它们的连接期间,并且不能在该连接之外引用。

当连接关闭时,表的行将被删除,并且临时表的内存描述将被删除。

供您参考 http://docs.oracle.com/javadb/10.6.2.1/ref/rrefdeclaretemptable.html


你所谈论的数据库产品与OP不同。对于Oracle数据库,请参考http://docs.oracle.com/database/121/CNCPT/tablecls.htm#CNCPT1138。在Oracle中,全局临时表是永久对象,用于存储临时会话特定(或事务特定)的数据。请参考https://dev59.com/SVDTa4cB1Zd3GeqPImGS。 - Shannon Severance

-2
  1. 通过在putty中运行以下命令关闭apache服务器 cd $ADMIN_SCRIPTS_HOME ./adstpall.sh
  2. 删除全局临时表 drop table t;

这样就可以了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接