【问题标题】:dropping a global temporary table删除全局临时表
【发布时间】:2011-12-17 11:49:51
【问题描述】:

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;

我只是不明白为什么 . 在这里。是语法还是什么?

【问题讨论】:

  • 表真的是全局临时表吗? (create global temporary table ....) 如果是这样,你为什么要放弃它?这是安装脚本的一部分吗?如果没有,也许全局临时表可以满足您的需求,而无需删除它。
  • 好吧,我们遇到了“已经存在”的问题,不知何故,它没有从 productino 环境中得到确认,表的状态是什么。此表不是安装脚本的一部分,它是单独过程的一部分。
  • 我不明白,你为什么遇到一个已经存在的全局临时表问题。该表应该已经存在,并且代码只是使用(插入、删除、更新等)它。
  • @Nader 的回答对我有用

标签: oracle plsql ddl temp-tables


【解决方案1】:
  1. 通过在putty 中运行来关闭 apache 服务器 cd $ADMIN_SCRIPTS_HOME ./adstpall.sh
  2. 删除全局临时表 drop table t;

这将锻炼..

【讨论】:

    【解决方案2】:
    -- 首先截断临时表 SQL> 截断表 test_temp1; -- 然后删除临时表 SQL> 删除表 test_temp1;

    【讨论】:

    • 重新连接(更改会话)后它在我的情况下工作。
    • 这是最佳答案!
    【解决方案3】:

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

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

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

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

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

    【讨论】:

    【解决方案4】:

    第 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
    

    【讨论】:

      【解决方案5】:

      是的 - 引擎会针对不同的条件抛出不同的异常。

      您将更改此部分以捕获异常并执行不同的操作

        EXCEPTION
            WHEN OTHERS THEN
      

      这是一个参考

      http://download.oracle.com/docs/cd/B10501_01/appdev.920/a96624/07_errs.htm

      【讨论】:

      猜你喜欢
      • 2010-12-07
      • 2015-12-02
      • 1970-01-01
      • 2010-10-15
      • 2018-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-02
      相关资源
      最近更新 更多