在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語句殺掉這些進程:
具體操作步驟,如下截圖所示:
原因: 查看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.