2011-10-28 82 views
10

2单独的问题。删除全球临时表

  1. 我使用这个脚本删除表[解决]

    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; 

最后我就是不明白为什么已经在这里了。它是语法还是什么?

+0

该表是否真的是全局临时表? ('创建全局临时表....')如果是这样,为什么你放弃它?这是安装脚本的一部分吗?如果没有,也许全球临时表可以满足您的需求,而无需丢弃它。 –

+0

好吧,我们遇到了“已经存在”的问题,不知何故,它没有从productino环境中得到证实,表格的状态如何。此表不是安装脚本的一部分,而是其单独过程的一部分。 –

+0

我不明白,你为什么遇到一个全球临时表已经存在的问题。该表应该已经存在,代码只是使用(插入,删除,更新等)它。 –

回答

12

第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:尝试 在已使用的临时表上创建,更改或删除索引

看错误是否为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 
-1

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

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

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

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

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

+0

您正在讨论与OP不同的数据库产品。对于Oracle数据库http://docs.oracle.com/database/121/CNCPT/tablecls.htm#CNCPT1138。 &“在Oracle中,全局临时表是存储临时会话特定(或特定事务)数据的永久对象。” http://stackoverflow.com/questions/3682360/sql-server-oracle-private-temporary-tables –

15
 
-- First Truncate temporary table 
SQL> TRUNCATE TABLE test_temp1; 

-- Then Drop temporary table 
SQL> DROP TABLE test_temp1; 
+2

此解决方案不起作用。 – zygimantus

+0

它在重新连接(更改会话)后在我的情况下工作。 –

0
  1. 下的Apache服务器在putty cd $ADMIN_SCRIPTS_HOME ./adstpall.sh
  2. 删除全局临时表 drop table t;

这将锻炼下方运行。