在 Oracle 資料庫中,當嘗試修改會話級全域暫存表(Global Temporary Table, 定義為 ON COMMIT PRESERVE ROWS)的結構時,常會遇到 ORA-14450: attempt to access a transactional temp table already in use 錯誤。這是因為該暫存表目前仍有資料被某個交易或會話持有,導致無法執行 DDL 操作(如 ALTER TABLE)。
SQL> CREATE GLOBAL TEMPORARY TABLE TEMP_TEST (
2 NAME VARCHAR2(12)
3 ) 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
若只有目前會話在使用該表,可執行 TRUNCATE TABLE 清空資料,即可釋放鎖定:
SQL> TRUNCATE TABLE TEMP_TEST; Table truncated. SQL> ALTER TABLE TEMP_TEST ADD SEX NUMBER(1); Table altered.
實務上常遇到其他會話(如應用程式連線)正在使用該暫存表,無法要求所有會話手動執行 TRUNCATE。此時需由 DBA 強制終止佔用該表的會話。
SELECT OWNER, OBJECT_ID, OBJECT_TYPE FROM DBA_OBJECTS WHERE OBJECT_NAME = '&OBJECT_NAME' AND OBJECT_TYPE = 'TABLE';
SELECT ADDR, KADDR, SID, LMODE FROM V$LOCK WHERE ID1 = &OBJECT_ID;
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;
執行上一步產生的 ALTER SYSTEM KILL SESSION ... IMMEDIATE; 指令,隨後即可順利執行 ALTER TABLE。
[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.
TRUNCATE TABLE 即可解決。V$LOCK、V$SESSION 找出佔用會話並 KILL SESSION。