【问题标题】:How to use Oracle global temporary table?如何使用 Oracle 全局临时表?
【发布时间】:2013-12-20 15:18:05
【问题描述】:

我正在尝试使用 Oracle 全局临时表,而无需在数据库中实际创建表。以下代码不起作用。有人可以解释一下使用全局临时表的正确方法吗?

declare
  global temporary table my_temp_table(column1 number) on commit preserve rows;    
begin
  insert into my_temp_table (column1) values (1);
  select * from my_temp_table;   
end;

【问题讨论】:

  • 在磁盘上创建有什么问题?
  • 全局临时表必须在数据库中创建,不能在PL/SQL本地定义。还有其他方法可以在 PL/SQL 中保存数据,使用 collections。合适的工具取决于你想要做什么——为什么你首先想要一个 GTT?您放入其中的数据的最终目标是什么?
  • @AlexPoole:link 有一个为会话声明临时表的功能,请找到链接,我不知道这是如何使用的,但喜欢探索
  • @GauravSoni - 这是 Derby 的文档,而不是 Oracle RDBMS?
  • @AlexPoole:啊,很抱歉,可能马修也因此而感到困惑。

标签: oracle plsql oracle11g oracle10g plsqldeveloper


【解决方案1】:

使用立即执行尝试以下操作:如果表已经存在,它使用异常处理程序绕过;另请注意,您不能在 PLSQL 中使用 SQL 选择

DECLARE
  l_column1 number;
begin
  begin
    execute immediate 'create global temporary table my_temp_table(column1 number) 
on commit   preserve rows';
  exception when others
    then
    dbms_output.put_line(sqlerrm);
  end;
  insert into my_temp_table (column1) values (1);
  select * into l_column1 from my_temp_table where column1=1;
  dbms_output.put_line('the temp value is '||l_column1);   
end;

【讨论】:

    【解决方案2】:

    除非您使用 EXECUTE IMMEDIATE,否则您无法在 PL/SQL 中创建表。试试这个:

    create global temporary table my_temp_table(column1 number) on commit preserve rows;    
    
    insert into my_temp_table (column1) values (1);
    select * from my_temp_table;   
    

    【讨论】:

    • oracle-base.com 上面评论 1 中的文章参考然后这解释了它的使用示例,当会话断开连接后再次连接时会发生什么。其数据将因此被清除。
    • 使用它的主要好处是减少生成的重做。撤消仍将与常规表相同。
    【解决方案3】:

    Oracle 全局临时表与您预期的略有不同。

    您需要创建表并将其声明为全局临时表。

    这是一个很好的资源: http://www.oracle-base.com/articles/misc/temporary-tables.php

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-02
      • 2023-03-27
      • 1970-01-01
      • 2016-04-02
      • 1970-01-01
      • 2015-07-27
      • 2012-02-13
      • 1970-01-01
      相关资源
      最近更新 更多