代码改变世界

ORA-14450: attempt to access a transactional temp table already in use

2014-10-26 10:39  潇湘隐者  阅读(5532)  评论(0编辑  收藏  举报

在ORACLE数据中修改会话级临时表时,有可能会遇到ORA-14550错误,那么为什么会话级全局临时表会报ORA-14450错误呢,如下所示,我们先从一个小小案例入手:

案例1:

SQL> CREATE GLOBAL TEMPORARY TABLE TEMP_TEST
  2  (
  3     NAME VARCHAR2(12)
  4  ) ON COMMIT PRESERVE ROWS;
 
Table created.
 
SQL> INSERT INTO TEMP_TEST VALUES('kerry');
 
1 row created.
 
SQL> COMMIT;
 
Commit complete.
 
SQL> ALTER TABLE TEMP_TEST ADD SEX NUMBER(1) ;
ALTER TABLE TEMP_TEST ADD SEX NUMBER(1)
*
ERROR at line 1:
ORA-14450: attempt to access a transactional temp table already in use

如上所示,修改会话级临时表时遇到了ORA-14450错误,那么有哪些解决方法呢? 这时需要断开会话或执行TRUNCATE语句:

SQL> TRUNCATE TABLE TEMP_TEST;
 
Table truncated.
 
SQL>  ALTER TABLE TEMP_TEST ADD SEX NUMBER(1) ;
 
Table altered.
 
SQL>

如下所示,我们模拟一个会话在操作临时表TEMP_TEST, 另外一个会话准备修改它,如下所示(实际场景可能更复杂,可能涉及多个会话而不是仅仅两个会话)

会话1:

SQL> SET SQLPROMPT "SQLSESSION 1 >"
SQLSESSION 1 >CREATE GLOBAL TEMPORARY TABLE TEMP_TEST
  2           (
  3              NAME VARCHAR2(12)
  4           ) ON COMMIT PRESERVE ROWS;
 
Table created.
 
SQLSESSION 1 >INSERT INTO TEMP_TEST VALUES('kerry');
 
1 row created.
 
SQLSESSION 1 >COMMIT;
 
Commit complete.
 
SQLSESSION 1 >

会话2::

SQL> SET SQLPROMPT "SESSION 2 >"
SESSION 2 >ALTER TABLE TEMP_TEST ADD SEX NUMBER(1) ;
ALTER TABLE TEMP_TEST ADD SEX NUMBER(1)
*
ERROR at line 1:
ORA-14450: attempt to access a transactional temp table already in use
 
 
SESSION 2 >

那么此时,会话1是其它用户登录的。比如应用程序等,你不可能要求所有会话都去执行TRUNCATE操作,这个时候该怎么处理呢?

此时你可以使用下面步骤解决这个问题。

Step 1、以sys或system登录数据库,先从DBA_OBJECTS中查询到该表的OBJECT_ID:

SELECT OWNER, OBJECT_ID,OBJECT_TYPE 
  FROM DBA_OBJECTS 
    WHERE OBJECT_NAME=&OBJECT_NAME AND OBJECT_TYPE ='TABLE';

Step 2、根据查到的OBJECT_ID知道使用该表的SESSION:

SELECT ADDR, KADDR, SID,LMODE FROM V$LOCK WHERE ID1=&OBJECT_ID;

Step 3、通过下面SQL找到对应的会话并生成KILL SESSION的执行语句

SET COL kill_session FOR A80;
select a.sid, a.serial#,a.status,
       a.paddr, 'alter system kill session ''' 
                    || a.sid || ',' || a.serial# 
                    || ''' immediate;' AS kill_session
FROM v$session a
WHERE a.sid in (select sid from v$enqueue_lock t where t.type='TO') 
  and a.sid=&sid;

Step 4、查看会话状态,并执行ALTER SYSTEM KILL SESSION语句杀掉这些进程:

具体操作步骤,如下截图所示: 

clip_image002


原因: 查看ORA-14450的错误,你可以看到如下信息:

[oracle@oracle-server ~]$ oerr ora 14450
14450, 00000, "attempt to access a transactional temp table already in use"
// *Cause:  An attempt was made to access a transactional temporary table that
//          has been already populated by a concurrent transaction of the same
//          session.
// *Action: do not attempt to access the temporary table until the
//          concurrent transaction has committed or aborted.