Example of Global Temporary Tables in PL/SQL
declare
type mycur is ref cursor;
l_gtname varchar2(30) default 'Global_table_' || userenv('sessionid');
l_cursor mycur;
l_ename varchar2(50);
l_cnt number := 0;
begin
execute immediate 'create global temporary table ' ||
l_gtname || ' on commit delete rows
as
select * from scott.emp where 1=0 ';
execute immediate 'insert into ' || l_gtname ||
' select * from scott.emp where rownum = 1';
open l_cursor for
'select ename from ' || l_gtname || ' order by ename';
loop
l_cnt := l_cnt+1;
fetch l_cursor into l_ename;
exit when l_cnt = 10; --l_cursor%notfound;
dbms_output.put_line( l_cnt||'-'||l_ename );
end loop;
close l_cursor;
dbms_output.put_line(l_gtname);
execute immediate 'drop table ' || l_gtname;
end;