删除全局临时表

x.5*_*509 11 oracle ddl plsql temp-tables

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;
    /
    
    Run Code Online (Sandbox Code Playgroud)

无论如何,如果表"不存在"或者在某些其他会话中使用它(在这种情况下它将被锁定且无法删除),我可以区分.我不确定我是否可以在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 ;
Run Code Online (Sandbox Code Playgroud)

我明白了.在这样的每个程序结束时

END PROCEDURE_NAME;
.
/
sho err;
Run Code Online (Sandbox Code Playgroud)

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

小智 16

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

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


Sha*_*nce 15

步骤1.确定要捕获的错误:

如果表不存在:

SQL> drop table x;
drop table x
           *
ERROR at line 1:
ORA-00942: table or view does not exist
Run Code Online (Sandbox Code Playgroud)

如果表正在使用中:

SQL> create global temporary table t (data varchar2(4000));

Table created.
Run Code Online (Sandbox Code Playgroud)

在另一个会话中使用该表.(注意插入后没有提交或任何内容.)

SQL> insert into t values ('whatever');

1 row created.
Run Code Online (Sandbox Code Playgroud)

回到第一个会话,尝试删除:

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
Run Code Online (Sandbox Code Playgroud)

陷阱的两个错误:

  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;
Run Code Online (Sandbox Code Playgroud)

结果,首先没有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.
Run Code Online (Sandbox Code Playgroud)

现在,随着t使用:

SQL> create global temporary table t (data varchar2(4000));

Table created.
Run Code Online (Sandbox Code Playgroud)

在另一场会议中:

SQL> insert into t values (null);

1 row created.
Run Code Online (Sandbox Code Playgroud)

然后在第一次会议中:

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
Run Code Online (Sandbox Code Playgroud)