我使用jdbc和oracle db。如何使用rt ON COMMIT PRESERVE ROWS创建全局临时表,然后使用另一个语句删除该表? 我的代码示例如下所示。
queryOfStatement1 = "CREATE GLOBAL TEMORARY TABLE MY_TABLE (ID VARCHAR(20 BYTE), NAME VARCHAR(20 BYTE)) ON COMMIT PRESERVE ROWS"; statement1.execute(); statement1.commit(); queryOfStatement2 = "DROP TABLE MY_TABLE"; statement2.execute(); statement2.commit();
答案 0 :(得分:0)
如Oracle官方文档中所述:“临时表在要缓冲结果集(暂时保留)的应用程序中很有用。在会话期间,行程数据是私有的。在会话结束时,可选行程被放弃了。“
您无法在同一会话中更改表定义,您必须断开连接才能更改或删除临时表。
在你的例子中:
SQL> show user
USER is "Z_TEST"
SQL>
SQL> CREATE GLOBAL TEMPORARY TABLE MY_TABLE
2 (
3 ID VARCHAR(20 BYTE),
4 NAME VARCHAR(20 BYTE)
5 )
6 ON COMMIT PRESERVE ROWS ;
Table created.
SQL> insert into my_table values ('this is id','this name') ;
1 row created.
SQL> commit ;
Commit complete.
SQL>
/* alter table example */
SQL>alter table
my_table
add
(col3 varchar2(100));
ERROR at line 1:
ORA-14450: attempt to access a transactional temp table already in use
SQL> drop table my_table ;
drop table my_table
*
ERROR at line 1:
ORA-14452: attempt to create, alter or drop an index on temporary table already in use
SQL>
SQL> show user
USER is "Z_TEST"
SQL> select * from my_table ;
ID NAME
-------------------- --------------------
this is id this name
SQL>
/* DISCONNECT SESSION AND CONNECT AGAIN */
SQL> conn z_test/welcome1
Connected.
SQL> show user
USER is "Z_TEST"
SQL> select * from my_table ;
no rows selected
SQL>
SQL> alter table
my_table
add
(col3 varchar2(100)) ;
Table altered.
SQL>
SQL> drop table my_table ;
Table dropped.
SQL>
重要的是要注意,当你断开/连接时,你会发现和“空表”,正如我在开头所说:“在会话结束时,可选的行程将被删除。”您的应用程序设计必须考虑此预期行为。