今天在对暂时表新增栏位时遇到了错误:ORA-14450 attempt to access a transactional temp table already inuse
以下依据实验模拟了2种暂时表使用不当报ORA-1445O的原因:
1、基于事物的暂时表
SQL> createglobal temporary tabletemp_tab on commit delete rowsas select 'a' as a1 fromdual;
Table created.
SQL> selectsid fromv$mystat where rownum=1;
SID
----------
191
SQL> insertintotemp_tab values('b');
1 row created.
SQL>
在191回话不做commit,打开另外一个session 运行ddl(假设在同一会话能够成功运行ddl语句)
SQL> altertabletemp_tab add b1varchar2(10);
alter tabletemp_tabadd b1 varchar2(10)
*
ERROR at line1:
ORA-14450: attempttoaccess a transactional temp table alreadyinuse
SQL> selectsid fromv$mystat where rownum=1;
SID
----------
40
SQL>
返回session 191 运行自己定义事物
SQL> declare
2 pragmaautonomous_transaction;
3 begin
4 insert intotemp_tab values ('c');
5 commit;
6 end;
7 /
declare
*
ERROR at line1:
ORA-14450: attempttoaccess a transactional temp table alreadyinuse
ORA-06512:at line 4
2、基于session 的暂时表
SQL> droptabletemp_tab purge;
Table dropped.
SQL> createglobal temporary tabletemp_tab on commit preserve rowsas select 'a' as a1 fromdual;
Table created.
SQL> selectsid fromv$mystat where rownum=1;
SID
----------
191
SQL> insertintotemp_tab values('a');
1 row created.
SQL> altertabletemp_tab add b1varchar2(10);
alter tabletemp_tabadd b1 varchar2(10)
*
ERROR at line1:
ORA-14450: attempttoaccess a transactional temp table alreadyinuse
SQL> select *fromtemp_tab;
A
-
a
a
SQL> commit;
Commit complete.
SQL> altertabletemp_tab add b1varchar2(10);
alter tabletemp_tabadd b1 varchar2(10)
*
ERROR at line1:
ORA-14450: attempttoaccess a transactional temp table alreadyinuse
上面实验能够看出基于session 的暂时表在同一个session内执行一个事物,不管是提交还是未提交都不能做ddl操作,再执行一个事物,不提交到另外一个session做ddl报错相同错误
SQL> deletefrom temp_tab;
2 rows deleted.
SQL> selectsid fromv$mystat where rownum=1;
SID
----------
40
SQL> altertabletemp_tab add b1varchar2(10);
alter tabletemp_tabadd b1 varchar2(10)
*
ERROR at line1:
ORA-14450: attempttoaccess a transactional temp table alreadyinuse
提交之后,在另外session測试发现还是无法ddl
SQL> commit;
Commit complete.
退出session,仅有一个会话做ddl
SQL> altertabletemp_tab add b1varchar2(10);
Table altered.
3、总结:
A、基于transaction的暂时表在同一个session里面不同意做自己定义事物,而且若在一个session里面执行了一条事物未做提交,在另外一个session无法做ddl操作
B、基于session的暂时表若在某一个sesson里面执行了事物,不论什么session都不执行做ddl包含执行该事物的session
4、解决ORA-14450错误
--查找sid及serial#
SQL> SELECTDISTINCT a.sid,a.SERIAL#FROMv$session a,v$sql b,v$enqueue_lockc
2 WHERE a.SID=c.SIDAND c.TYPE='TO'AND a.MODULE_HASH=b.MODULE_HASHANDlower(b.SQL_TEXT)LIKE '%tmp_tab%';
SID SERIAL#
---------- ----------
191 264
40 166
--使用sys用户登陆
SQL> altersystem kill session '191,264';
System altered
SQL> altersystem kill session '40,166';
SQL>
--回到scott
SQL> ALTERTABLE temp_tabADD cNUMBER(2) ;
Table altered