在oracle的存储过程中,不能直接使用DDL语句,比方create、alter、drop、truncate等。
那假设我们想在存储过程中建立一张暂时表就仅仅能使用动态sql语句了:
create or replace procedure pro as
  str_sql varchar2(100);
begin
  -- 创建暂时表
  str_sql := ‘create global temporary table temp_table (
       col1 varchar2(10),
       col2 number
    ) on commit preserve rows‘;
  execute immediate str_sql;
  -- 使用暂时表
  str_sql := ‘insert into temp_table(col1, col2) values(‘‘a‘‘, 1)‘;
  execute immediate str_sql;
  -- 删除暂时表
  str_sql := ‘drop table temp_table‘;
  execute immediate str_sql;
end;
在oracle中。暂时表分为会话级别(session)和事务级别(transaction)两种。
会话级的暂时表在整个会话期间都存在,直到会话结束;事务级别的暂时表数据在transaction结束后消失。即commit/rollback或结束会话时,
会清除暂时表数据。
on commit preserve rows -- 会话级别暂时表(退出登录会结束会话)
on commit delete rows -- 事务级别暂时表(提交或回滚会结束事务)
暂时表优缺点:
1. 在只查询数据时建议使用游标。
2. 暂时表不会建立索引。所以假设数据量比較大或进行多次查询时,不推荐使用。
原文:http://www.cnblogs.com/llguanli/p/7082099.html