oracle中的臨時表用法彙總

2021-04-06 19:36:43 字數 2077 閱讀 4668

oracle

中的臨時表用法彙總 說明

:下文中的一些說明和示例**摘自

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 rows

說明臨時表是事務指定,每次提交後

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

過程已成功完成。  

已用時間

: 00: 00: 00.79

15:24:08 sql> desc t_temp;

error:

ora-04043:

物件t_temp

不存在  

3 特性和效能

(與普通表和檢視的比較)

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

臨時表不建立索引

,所以如果資料量比較大或進行多次查詢時

,不推薦使用

資料處理比較複雜的時候時表快

,反之檢視快點

在僅僅查詢資料的時候建議用游標

: open cursor for ''sql clause'';

Oracle中的臨時表用法彙總

1 語法 在oracle中,可以建立以下兩種臨時表 1 會話特有的臨時表 create global temporary on commit preserve rows 2 事務特有的臨時表 create global temporary on commit delete rows create g...

ORACLE 臨時表用法

create global temporary table tablename col1 varchar2 10 col2 number on commit preserve delete rows 這種臨時表不占用表空間,而且不同的session之間互相看不到對方的資料 在會話結束後表中的資料自動...

Oracle 臨時表用法

oracle的臨時表在應用系統中有很大的作用,它可以讓使用者只能夠操作各自的資料中而互不干擾,不用擔心會破壞或影響其他session transaction的資料,這也是資料安全的一種解決方法。臨時表分為session transaction兩種,session級的臨時表資料在整個session都存...