如何使用Oracle全局临时表?

Dee*_*ent 5 oracle plsql oracle10g oracle11g plsqldeveloper

我正在尝试使用 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;
Run Code Online (Sandbox Code Playgroud)

psa*_*j12 4

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

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;
Run Code Online (Sandbox Code Playgroud)