oracle中的临时表用法汇总

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'';

  

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值