在Oracle資料庫中的臨時表的用法彙總

2021-09-08 20:52:32 字數 1792 閱讀 1159

在oracle資料庫中的臨時表的用法彙總

2004-11-11 16:41:00

說明:下文中的一些說明和示例**摘自csdn,恕不一一指明出處,在此一併對相關作者表示感謝!

1 語法

在oracle中,可以建立以下兩種臨時表:

1) 會話特有的臨時表

create global temporary ( )

on commit preserve rows;

2) 事務特有的臨時表

create global temporary ( )

on commit delete rows;

create global temporary table mytemptable

所建的臨時表雖然是存在的,但是如果insert 一條記錄然後用別的連線登上去select,記錄是空的。  

--on commit delete ro

ws 說明臨時表是事務指定,每次提交後oracle將截斷表(刪除全部行) --on commit preserve rows 說明臨時表是會話指定,當中斷會話時oracle將截斷表。  

2 動態建立

create or replace procedure pro_temp(v_col1 varchar2,v_col2 varchar2) as

v_num number;

begin

select count(*) into v_num from user_tables where table_name='t_temp';  

--create temporary table

if v_num<1 then

execute immediate 'create global temporary table t_temp (

col1 varchar2(10),

col2 varchar2(10)

) on commit delete rows';

end if;  

--insert data

execute immediate 'insert into t_temp values('''  v_col1  ''','''  v_col2  ''')';  

execute immediate 'select col1 from t_temp' into v_num;

dbms_output.put_line(v_num);

execute immediate 'delete from t_temp';

commit;

execute immediate 'drop table t_temp';

end pro_temp;  

測試:  

15:23:54 sql> set serveroutput on

15:24:01 sql> exec pro_temp('11','22');

11  

pl/sql 過程已成功完成。  

15:24:08 sql> desc t_temp;

error:

ora-04043: 物件 t_temp 不存在  

3 特性和效能(與普通表和檢視的比較)

臨時表只在當前連線內有效

臨時表不建立索引,所以如果資料量比較大或進行多次查詢時,不推薦使用

資料處理比較複雜的時候時表快,反之檢視快點

在僅僅查詢資料的時候建議用游標: open cursor for 'sql clause';

資料庫中建立臨時表

語法 create table albums artist char 30 album name char 50 media type int go該錶也可以使用下邊的命令來手動刪除 drop table albums go當使用者退出sql server 時該表也可以被自動地刪除如果你是在自態sq...

oracle清理資料庫的臨時表空間

1.startup 啟動資料庫 2.create temporary tablespace temp2 tempfile home2 oracle oradata sysmon temp02.dbf size 512m reuse autoextend on next 640k maxsize un...

oracle清理資料庫的臨時表空間

1.startup 啟動資料庫 2.create temporary tablespace temp2 tempfile home2 oracle oradata sysmon temp02.dbf size 512m reuse autoextend on next 640k maxsize un...