1.Oracle的临时表分为会话级临时表和事务级临时表
2.创建语法分别为
会话临时表:
CREATE GLOBAL Temporary TABLE TEMPTABLENAME
ON COMMIT PRESERVE ROWS
AS
SELECT .....FROM..
事务临时表:
CREATE GLOBAL Temporary TABLE TEMPTABLENAME
ON COMMIT DELETE ROWS
INSERT INTO TEMPTABLENAME VALUES('','',....)
3.删除临时表
会话临时表在使用TRUNCATE 或 会话结束后删除掉
事务临时表在COMMIT和ROLLBACK后自动删除
存在前面两个前提时,直接用DROP TABLE TEMPTABLENAME
4.提示会话在使用删除不了,则需要结束了会话才能DROP会话临时表
SELECT sid, serial#
FROM v$session
WHERE sid = (SELECT sid FROM v$lock
WHERE id1 = (SELECT object_id FROM dba_objects
WHERE object_name = UPPER('TEMPTABLENAME3')));
ALTER system kill session '751,16962';
5.批量删除临时表
declare p_sql varchar(200);
begin
for r in (select a.OBJECT_NAME from ALL_OBJECTS a where a.OBJECT_NAME like 'TEMPTABLENAME%') loop
p_sql:='TRUNCATE TABLE '||r.OBJECT_NAME;
execute immediate p_sql;
p_sql:='drop table '||r.OBJECT_NAME;
execute immediate p_sql;
end loop;
end;